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$");
48 #include "archive_private.h"
49 #include "archive_read_private.h"
51 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
55 size_t out_block_size;
56 char valid; /* True = decompressor is initialized */
57 char eof; /* True = found end of compressed data. */
61 static ssize_t bzip2_filter_read(struct archive_read_filter *, const void **);
62 static int bzip2_filter_close(struct archive_read_filter *);
66 * Note that we can detect bzip2 archives even if we can't decompress
67 * them. (In fact, we like detecting them because we can give better
68 * error messages.) So the bid framework here gets compiled even
69 * if bzlib is unavailable.
71 static int bzip2_reader_bid(struct archive_read_filter_bidder *, struct archive_read_filter *);
72 static int bzip2_reader_init(struct archive_read_filter *);
73 static int bzip2_reader_free(struct archive_read_filter_bidder *);
75 #if ARCHIVE_VERSION_NUMBER < 4000000
76 /* Deprecated; remove in libarchive 4.0 */
78 archive_read_support_compression_bzip2(struct archive *a)
80 return archive_read_support_filter_bzip2(a);
85 archive_read_support_filter_bzip2(struct archive *_a)
87 struct archive_read *a = (struct archive_read *)_a;
88 struct archive_read_filter_bidder *reader;
90 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
91 ARCHIVE_STATE_NEW, "archive_read_support_filter_bzip2");
93 if (__archive_read_get_bidder(a, &reader) != ARCHIVE_OK)
94 return (ARCHIVE_FATAL);
97 reader->bid = bzip2_reader_bid;
98 reader->init = bzip2_reader_init;
99 reader->options = NULL;
100 reader->free = bzip2_reader_free;
101 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
104 archive_set_error(_a, ARCHIVE_ERRNO_MISC,
105 "Using external bunzip2 program");
106 return (ARCHIVE_WARN);
111 bzip2_reader_free(struct archive_read_filter_bidder *self){
112 (void)self; /* UNUSED */
117 * Test whether we can handle this data.
119 * This logic returns zero if any part of the signature fails. It
120 * also tries to Do The Right Thing if a very short buffer prevents us
121 * from verifying as much as we would like.
124 bzip2_reader_bid(struct archive_read_filter_bidder *self, struct archive_read_filter *filter)
126 const unsigned char *buffer;
130 (void)self; /* UNUSED */
132 /* Minimal bzip2 archive is 14 bytes. */
133 buffer = __archive_read_filter_ahead(filter, 14, &avail);
137 /* First three bytes must be "BZh" */
139 if (memcmp(buffer, "BZh", 3) != 0)
143 /* Next follows a compression flag which must be an ASCII digit. */
144 if (buffer[3] < '1' || buffer[3] > '9')
148 /* After BZh[1-9], there must be either a data block
149 * which begins with 0x314159265359 or an end-of-data
150 * marker of 0x177245385090. */
151 if (memcmp(buffer + 4, "\x31\x41\x59\x26\x53\x59", 6) == 0)
153 else if (memcmp(buffer + 4, "\x17\x72\x45\x38\x50\x90", 6) == 0)
158 return (bits_checked);
161 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
164 * If we don't have the library on this system, we can't actually do the
165 * decompression. We can, however, still detect compressed archives
166 * and emit a useful message.
169 bzip2_reader_init(struct archive_read_filter *self)
173 r = __archive_read_program(self, "bunzip2");
174 /* Note: We set the format here even if __archive_read_program()
175 * above fails. We do, after all, know what the format is
176 * even if we weren't able to read it. */
177 self->code = ARCHIVE_COMPRESSION_BZIP2;
178 self->name = "bzip2";
186 * Setup the callbacks.
189 bzip2_reader_init(struct archive_read_filter *self)
191 static const size_t out_block_size = 64 * 1024;
193 struct private_data *state;
195 self->code = ARCHIVE_COMPRESSION_BZIP2;
196 self->name = "bzip2";
198 state = (struct private_data *)calloc(sizeof(*state), 1);
199 out_block = (unsigned char *)malloc(out_block_size);
200 if (state == NULL || out_block == NULL) {
201 archive_set_error(&self->archive->archive, ENOMEM,
202 "Can't allocate data for bzip2 decompression");
205 return (ARCHIVE_FATAL);
209 state->out_block_size = out_block_size;
210 state->out_block = out_block;
211 self->read = bzip2_filter_read;
212 self->skip = NULL; /* not supported */
213 self->close = bzip2_filter_close;
219 * Return the next block of decompressed data.
222 bzip2_filter_read(struct archive_read_filter *self, const void **p)
224 struct private_data *state;
226 const char *read_buf;
229 state = (struct private_data *)self->data;
236 /* Empty our output buffer. */
237 state->stream.next_out = state->out_block;
238 state->stream.avail_out = state->out_block_size;
240 /* Try to fill the output buffer. */
243 if (bzip2_reader_bid(self->bidder, self->upstream) == 0) {
245 *p = state->out_block;
246 decompressed = state->stream.next_out
248 return (decompressed);
250 /* Initialize compression library. */
251 ret = BZ2_bzDecompressInit(&(state->stream),
252 0 /* library verbosity */,
253 0 /* don't use low-mem algorithm */);
255 /* If init fails, try low-memory algorithm instead. */
256 if (ret == BZ_MEM_ERROR)
257 ret = BZ2_bzDecompressInit(&(state->stream),
258 0 /* library verbosity */,
259 1 /* do use low-mem algo */);
262 const char *detail = NULL;
263 int err = ARCHIVE_ERRNO_MISC;
266 detail = "invalid setup parameter";
270 detail = "out of memory";
272 case BZ_CONFIG_ERROR:
273 detail = "mis-compiled library";
276 archive_set_error(&self->archive->archive, err,
277 "Internal error initializing decompressor%s%s",
278 detail == NULL ? "" : ": ",
280 return (ARCHIVE_FATAL);
285 /* stream.next_in is really const, but bzlib
286 * doesn't declare it so. <sigh> */
288 __archive_read_filter_ahead(self->upstream, 1, &ret);
289 if (read_buf == NULL) {
290 archive_set_error(&self->archive->archive,
292 "truncated bzip2 input");
293 return (ARCHIVE_FATAL);
295 state->stream.next_in = (char *)(uintptr_t)read_buf;
296 state->stream.avail_in = ret;
297 /* There is no more data, return whatever we have. */
300 *p = state->out_block;
301 decompressed = state->stream.next_out
303 return (decompressed);
306 /* Decompress as much as we can in one pass. */
307 ret = BZ2_bzDecompress(&(state->stream));
308 __archive_read_filter_consume(self->upstream,
309 state->stream.next_in - read_buf);
312 case BZ_STREAM_END: /* Found end of stream. */
313 switch (BZ2_bzDecompressEnd(&(state->stream))) {
317 archive_set_error(&(self->archive->archive),
319 "Failed to clean up decompressor");
320 return (ARCHIVE_FATAL);
324 case BZ_OK: /* Decompressor made some progress. */
325 /* If we filled our buffer, update stats and return. */
326 if (state->stream.avail_out == 0) {
327 *p = state->out_block;
328 decompressed = state->stream.next_out
330 return (decompressed);
333 default: /* Return an error. */
334 archive_set_error(&self->archive->archive,
335 ARCHIVE_ERRNO_MISC, "bzip decompression failed");
336 return (ARCHIVE_FATAL);
342 * Clean up the decompressor.
345 bzip2_filter_close(struct archive_read_filter *self)
347 struct private_data *state;
348 int ret = ARCHIVE_OK;
350 state = (struct private_data *)self->data;
353 switch (BZ2_bzDecompressEnd(&state->stream)) {
357 archive_set_error(&self->archive->archive,
359 "Failed to clean up decompressor");
365 free(state->out_block);
370 #endif /* HAVE_BZLIB_H && BZ_CONFIG_ERROR */