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.14 2007/03/24 03:23:26 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 return (__archive_read_register_compression(a, bid, init));
78 * Test whether we can handle this data.
80 * This logic returns zero if any part of the signature fails. It
81 * also tries to Do The Right Thing if a very short buffer prevents us
82 * from verifying as much as we would like.
85 bid(const void *buff, size_t len)
87 const unsigned char *buffer;
93 buffer = (const unsigned char *)buff;
95 if (buffer[0] != 'B') /* Verify first ID byte. */
99 return (bits_checked);
101 if (buffer[1] != 'Z') /* Verify second ID byte. */
105 return (bits_checked);
107 if (buffer[2] != 'h') /* Verify third ID byte. */
111 return (bits_checked);
113 if (buffer[3] < '1' || buffer[3] > '9')
118 * Research Question: Can we do any more to verify that this
119 * really is BZip2 format?? For 99.9% of the time, the above
120 * test is sufficient, but it would be nice to do a more
121 * thorough check. It's especially troubling that the BZip2
122 * signature begins with all ASCII characters; a tar archive
123 * whose first filename begins with 'BZh3' would potentially
124 * fool this logic. (It may also be possible to guard against
125 * such anomalies in archive_read_support_compression_none.)
128 return (bits_checked);
134 * If we don't have bzlib on this system, we can't actually do the
135 * decompression. We can, however, still detect bzip2-compressed
136 * archives and emit a useful message.
139 init(struct archive_read *a, const void *buff, size_t n)
141 (void)a; /* UNUSED */
142 (void)buff; /* UNUSED */
143 (void)n; /* UNUSED */
145 archive_set_error(a, -1,
146 "This version of libarchive was compiled without bzip2 support");
147 return (ARCHIVE_FATAL);
154 * Setup the callbacks.
157 init(struct archive_read *a, const void *buff, size_t n)
159 struct private_data *state;
162 a->archive.compression_code = ARCHIVE_COMPRESSION_BZIP2;
163 a->archive.compression_name = "bzip2";
165 state = (struct private_data *)malloc(sizeof(*state));
167 archive_set_error(&a->archive, ENOMEM,
168 "Can't allocate data for %s decompression",
169 a->archive.compression_name);
170 return (ARCHIVE_FATAL);
172 memset(state, 0, sizeof(*state));
174 state->uncompressed_buffer_size = 64 * 1024;
175 state->uncompressed_buffer = (char *)malloc(state->uncompressed_buffer_size);
176 state->stream.next_out = state->uncompressed_buffer;
177 state->read_next = state->uncompressed_buffer;
178 state->stream.avail_out = state->uncompressed_buffer_size;
180 if (state->uncompressed_buffer == NULL) {
181 archive_set_error(&a->archive, ENOMEM,
182 "Can't allocate %s decompression buffers",
183 a->archive.compression_name);
185 return (ARCHIVE_FATAL);
189 * A bug in bzlib.h: stream.next_in should be marked 'const'
190 * but isn't (the library never alters data through the
191 * next_in pointer, only reads it). The result: this ugly
192 * cast to remove 'const'.
194 state->stream.next_in = (char *)(uintptr_t)(const void *)buff;
195 state->stream.avail_in = n;
197 a->compression_read_ahead = read_ahead;
198 a->compression_read_consume = read_consume;
199 a->compression_skip = NULL; /* not supported */
200 a->compression_finish = finish;
202 /* Initialize compression library. */
203 ret = BZ2_bzDecompressInit(&(state->stream),
204 0 /* library verbosity */,
205 0 /* don't use slow low-mem algorithm */);
207 /* If init fails, try using low-memory algorithm instead. */
208 if (ret == BZ_MEM_ERROR) {
209 ret = BZ2_bzDecompressInit(&(state->stream),
210 0 /* library verbosity */,
211 1 /* do use slow low-mem algorithm */);
215 a->compression_data = state;
219 /* Library setup failed: Clean up. */
220 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
221 "Internal error initializing %s library",
222 a->archive.compression_name);
223 free(state->uncompressed_buffer);
226 /* Override the error message if we know what really went wrong. */
229 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
230 "Internal error initializing compression library: "
231 "invalid setup parameter");
234 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
235 "Internal error initializing compression library: "
238 case BZ_CONFIG_ERROR:
239 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
240 "Internal error initializing compression library: "
241 "mis-compiled library");
245 return (ARCHIVE_FATAL);
249 * Return a block of data from the decompression buffer. Decompress more
253 read_ahead(struct archive_read *a, const void **p, size_t min)
255 struct private_data *state;
256 int read_avail, was_avail, ret;
258 state = (struct private_data *)a->compression_data;
260 if (!a->client_reader) {
261 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
262 "No read callback is registered? "
263 "This is probably an internal programming error.");
264 return (ARCHIVE_FATAL);
267 read_avail = state->stream.next_out - state->read_next;
269 if (read_avail + state->stream.avail_out < min) {
270 memmove(state->uncompressed_buffer, state->read_next,
272 state->read_next = state->uncompressed_buffer;
273 state->stream.next_out = state->read_next + read_avail;
274 state->stream.avail_out
275 = state->uncompressed_buffer_size - read_avail;
278 while (was_avail < read_avail && /* Made some progress. */
279 read_avail < (int)min && /* Haven't satisfied min. */
280 read_avail < (int)state->uncompressed_buffer_size) { /* !full */
281 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
283 was_avail = read_avail;
284 read_avail = state->stream.next_out - state->read_next;
287 *p = state->read_next;
292 * Mark a previously-returned block of data as read.
295 read_consume(struct archive_read *a, size_t n)
297 struct private_data *state;
299 state = (struct private_data *)a->compression_data;
300 a->archive.file_position += n;
301 state->read_next += n;
302 if (state->read_next > state->stream.next_out)
303 __archive_errx(1, "Request to consume too many "
304 "bytes from bzip2 decompressor");
309 * Clean up the decompressor.
312 finish(struct archive_read *a)
314 struct private_data *state;
317 state = (struct private_data *)a->compression_data;
319 switch (BZ2_bzDecompressEnd(&(state->stream))) {
323 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
324 "Failed to clean up %s compressor",
325 a->archive.compression_name);
329 free(state->uncompressed_buffer);
332 a->compression_data = NULL;
333 if (a->client_closer != NULL)
334 (a->client_closer)(&a->archive, a->client_data);
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;
350 total_decompressed = 0;
352 if (state->stream.avail_in == 0) {
353 ret = (a->client_reader)(&a->archive, a->client_data,
354 (const void **)&state->stream.next_in);
357 * TODO: Find a better way to handle
362 if (ret == 0 && total_decompressed == 0) {
363 archive_set_error(&a->archive, EIO,
364 "Premature end of %s compressed data",
365 a->archive.compression_name);
366 return (ARCHIVE_FATAL);
368 a->archive.raw_position += ret;
369 state->stream.avail_in = ret;
373 output = state->stream.next_out;
375 /* Decompress some data. */
376 ret = BZ2_bzDecompress(&(state->stream));
377 decompressed = state->stream.next_out - output;
379 /* Accumulate the total bytes of output. */
380 state->total_out += decompressed;
381 total_decompressed += decompressed;
384 case BZ_OK: /* Decompressor made some progress. */
385 if (decompressed > 0)
388 case BZ_STREAM_END: /* Found end of stream. */
391 /* Any other return value is an error. */
398 /* Return a fatal error. */
400 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
401 "%s decompression failed", a->archive.compression_name);
402 return (ARCHIVE_FATAL);
405 #endif /* HAVE_BZLIB_H */