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: src/lib/libarchive/archive_read_support_compression_bzip2.c,v 1.15 2007/04/05 05:18:16 kientzle Exp $");
48 #include "archive_private.h"
49 #include "archive_read_private.h"
54 char *uncompressed_buffer;
55 size_t uncompressed_buffer_size;
60 static int finish(struct archive_read *);
61 static ssize_t read_ahead(struct archive_read *, const void **, size_t);
62 static ssize_t read_consume(struct archive_read *, size_t);
63 static int drive_decompressor(struct archive_read *a, struct private_data *);
66 /* These two functions are defined even if we lack bzlib. See below. */
67 static int bid(const void *, size_t);
68 static int init(struct archive_read *, const void *, size_t);
71 archive_read_support_compression_bzip2(struct archive *_a)
73 struct archive_read *a = (struct archive_read *)_a;
74 if (__archive_read_register_compression(a, bid, init) != NULL)
76 return (ARCHIVE_FATAL);
80 * Test whether we can handle this data.
82 * This logic returns zero if any part of the signature fails. It
83 * also tries to Do The Right Thing if a very short buffer prevents us
84 * from verifying as much as we would like.
87 bid(const void *buff, size_t len)
89 const unsigned char *buffer;
95 buffer = (const unsigned char *)buff;
97 if (buffer[0] != 'B') /* Verify first ID byte. */
101 return (bits_checked);
103 if (buffer[1] != 'Z') /* Verify second ID byte. */
107 return (bits_checked);
109 if (buffer[2] != 'h') /* Verify third ID byte. */
113 return (bits_checked);
115 if (buffer[3] < '1' || buffer[3] > '9')
120 * Research Question: Can we do any more to verify that this
121 * really is BZip2 format?? For 99.9% of the time, the above
122 * test is sufficient, but it would be nice to do a more
123 * thorough check. It's especially troubling that the BZip2
124 * signature begins with all ASCII characters; a tar archive
125 * whose first filename begins with 'BZh3' would potentially
126 * fool this logic. (It may also be possible to guard against
127 * such anomalies in archive_read_support_compression_none.)
130 return (bits_checked);
136 * If we don't have bzlib on this system, we can't actually do the
137 * decompression. We can, however, still detect bzip2-compressed
138 * archives and emit a useful message.
141 init(struct archive_read *a, const void *buff, size_t n)
143 (void)a; /* UNUSED */
144 (void)buff; /* UNUSED */
145 (void)n; /* UNUSED */
147 archive_set_error(a, -1,
148 "This version of libarchive was compiled without bzip2 support");
149 return (ARCHIVE_FATAL);
156 * Setup the callbacks.
159 init(struct archive_read *a, const void *buff, size_t n)
161 struct private_data *state;
164 a->archive.compression_code = ARCHIVE_COMPRESSION_BZIP2;
165 a->archive.compression_name = "bzip2";
167 state = (struct private_data *)malloc(sizeof(*state));
169 archive_set_error(&a->archive, ENOMEM,
170 "Can't allocate data for %s decompression",
171 a->archive.compression_name);
172 return (ARCHIVE_FATAL);
174 memset(state, 0, sizeof(*state));
176 state->uncompressed_buffer_size = 64 * 1024;
177 state->uncompressed_buffer = (char *)malloc(state->uncompressed_buffer_size);
178 state->stream.next_out = state->uncompressed_buffer;
179 state->read_next = state->uncompressed_buffer;
180 state->stream.avail_out = state->uncompressed_buffer_size;
182 if (state->uncompressed_buffer == NULL) {
183 archive_set_error(&a->archive, ENOMEM,
184 "Can't allocate %s decompression buffers",
185 a->archive.compression_name);
187 return (ARCHIVE_FATAL);
191 * A bug in bzlib.h: stream.next_in should be marked 'const'
192 * but isn't (the library never alters data through the
193 * next_in pointer, only reads it). The result: this ugly
194 * cast to remove 'const'.
196 state->stream.next_in = (char *)(uintptr_t)(const void *)buff;
197 state->stream.avail_in = n;
199 a->decompressor->read_ahead = read_ahead;
200 a->decompressor->consume = read_consume;
201 a->decompressor->skip = NULL; /* not supported */
202 a->decompressor->finish = finish;
204 /* Initialize compression library. */
205 ret = BZ2_bzDecompressInit(&(state->stream),
206 0 /* library verbosity */,
207 0 /* don't use slow low-mem algorithm */);
209 /* If init fails, try using low-memory algorithm instead. */
210 if (ret == BZ_MEM_ERROR) {
211 ret = BZ2_bzDecompressInit(&(state->stream),
212 0 /* library verbosity */,
213 1 /* do use slow low-mem algorithm */);
217 a->decompressor->data = state;
221 /* Library setup failed: Clean up. */
222 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
223 "Internal error initializing %s library",
224 a->archive.compression_name);
225 free(state->uncompressed_buffer);
228 /* Override the error message if we know what really went wrong. */
231 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
232 "Internal error initializing compression library: "
233 "invalid setup parameter");
236 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
237 "Internal error initializing compression library: "
240 case BZ_CONFIG_ERROR:
241 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
242 "Internal error initializing compression library: "
243 "mis-compiled library");
247 return (ARCHIVE_FATAL);
251 * Return a block of data from the decompression buffer. Decompress more
255 read_ahead(struct archive_read *a, const void **p, size_t min)
257 struct private_data *state;
258 size_t read_avail, was_avail;
261 state = (struct private_data *)a->decompressor->data;
262 if (!a->client_reader) {
263 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
264 "No read callback is registered? "
265 "This is probably an internal programming error.");
266 return (ARCHIVE_FATAL);
269 read_avail = state->stream.next_out - state->read_next;
271 if (read_avail + state->stream.avail_out < min) {
272 memmove(state->uncompressed_buffer, state->read_next,
274 state->read_next = state->uncompressed_buffer;
275 state->stream.next_out = state->read_next + read_avail;
276 state->stream.avail_out
277 = state->uncompressed_buffer_size - read_avail;
280 while (read_avail < min && /* Haven't satisfied min. */
281 read_avail < state->uncompressed_buffer_size) { /* !full */
282 was_avail = read_avail;
283 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
285 read_avail = state->stream.next_out - state->read_next;
286 if (was_avail == read_avail) /* No progress? */
290 *p = state->read_next;
295 * Mark a previously-returned block of data as read.
298 read_consume(struct archive_read *a, size_t n)
300 struct private_data *state;
302 state = (struct private_data *)a->decompressor->data;
303 a->archive.file_position += n;
304 state->read_next += n;
305 if (state->read_next > state->stream.next_out)
306 __archive_errx(1, "Request to consume too many "
307 "bytes from bzip2 decompressor");
312 * Clean up the decompressor.
315 finish(struct archive_read *a)
317 struct private_data *state;
320 state = (struct private_data *)a->decompressor->data;
322 switch (BZ2_bzDecompressEnd(&(state->stream))) {
326 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
327 "Failed to clean up %s compressor",
328 a->archive.compression_name);
332 free(state->uncompressed_buffer);
335 a->decompressor->data = NULL;
340 * Utility function to pull data through decompressor, reading input
341 * blocks as necessary.
344 drive_decompressor(struct archive_read *a, struct private_data *state)
347 int decompressed, total_decompressed;
349 const void *read_buf;
351 total_decompressed = 0;
353 if (state->stream.avail_in == 0) {
354 read_buf = state->stream.next_in;
355 ret = (a->client_reader)(&a->archive, a->client_data,
357 state->stream.next_in = (void *)(uintptr_t)read_buf;
360 * TODO: Find a better way to handle
365 if (ret == 0 && total_decompressed == 0) {
366 archive_set_error(&a->archive, EIO,
367 "Premature end of %s compressed data",
368 a->archive.compression_name);
369 return (ARCHIVE_FATAL);
371 a->archive.raw_position += ret;
372 state->stream.avail_in = ret;
376 output = state->stream.next_out;
378 /* Decompress some data. */
379 ret = BZ2_bzDecompress(&(state->stream));
380 decompressed = state->stream.next_out - output;
382 /* Accumulate the total bytes of output. */
383 state->total_out += decompressed;
384 total_decompressed += decompressed;
387 case BZ_OK: /* Decompressor made some progress. */
388 if (decompressed > 0)
391 case BZ_STREAM_END: /* Found end of stream. */
394 /* Any other return value is an error. */
401 /* Return a fatal error. */
403 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
404 "%s decompression failed", a->archive.compression_name);
405 return (ARCHIVE_FATAL);
408 #endif /* HAVE_BZLIB_H */