2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
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.
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.
27 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
49 #include "archive_private.h"
54 char *uncompressed_buffer;
55 size_t uncompressed_buffer_size;
60 static int finish(struct archive *);
61 static ssize_t read_ahead(struct archive *, const void **, size_t);
62 static ssize_t read_consume(struct archive *, size_t);
63 static int drive_decompressor(struct archive *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 *, const void *, size_t);
71 archive_read_support_compression_bzip2(struct archive *a)
73 return (__archive_read_register_compression(a, bid, init));
77 * Test whether we can handle this data.
79 * This logic returns zero if any part of the signature fails. It
80 * also tries to Do The Right Thing if a very short buffer prevents us
81 * from verifying as much as we would like.
84 bid(const void *buff, size_t len)
86 const unsigned char *buffer;
92 buffer = (const unsigned char *)buff;
94 if (buffer[0] != 'B') /* Verify first ID byte. */
98 return (bits_checked);
100 if (buffer[1] != 'Z') /* Verify second ID byte. */
104 return (bits_checked);
106 if (buffer[2] != 'h') /* Verify third ID byte. */
110 return (bits_checked);
112 if (buffer[3] < '1' || buffer[3] > '9')
117 * Research Question: Can we do any more to verify that this
118 * really is BZip2 format?? For 99.9% of the time, the above
119 * test is sufficient, but it would be nice to do a more
120 * thorough check. It's especially troubling that the BZip2
121 * signature begins with all ASCII characters; a tar archive
122 * whose first filename begins with 'BZh3' would potentially
123 * fool this logic. (It may also be possible to gaurd against
124 * such anomalies in archive_read_support_compression_none.)
127 return (bits_checked);
133 * If we don't have bzlib on this system, we can't actually do the
134 * decompression. We can, however, still detect bzip2-compressed
135 * archives and emit a useful message.
138 init(struct archive *a, const void *buff, size_t n)
140 (void)a; /* UNUSED */
141 (void)buff; /* UNUSED */
142 (void)n; /* UNUSED */
144 archive_set_error(a, -1,
145 "This version of libarchive was compiled without bzip2 support");
146 return (ARCHIVE_FATAL);
153 * Setup the callbacks.
156 init(struct archive *a, const void *buff, size_t n)
158 struct private_data *state;
161 a->compression_code = ARCHIVE_COMPRESSION_BZIP2;
162 a->compression_name = "bzip2";
164 state = (struct private_data *)malloc(sizeof(*state));
166 archive_set_error(a, ENOMEM,
167 "Can't allocate data for %s decompression",
168 a->compression_name);
169 return (ARCHIVE_FATAL);
171 memset(state, 0, sizeof(*state));
173 state->uncompressed_buffer_size = 64 * 1024;
174 state->uncompressed_buffer = (char *)malloc(state->uncompressed_buffer_size);
175 state->stream.next_out = state->uncompressed_buffer;
176 state->read_next = state->uncompressed_buffer;
177 state->stream.avail_out = state->uncompressed_buffer_size;
179 if (state->uncompressed_buffer == NULL) {
180 archive_set_error(a, ENOMEM,
181 "Can't allocate %s decompression buffers",
182 a->compression_name);
184 return (ARCHIVE_FATAL);
188 * A bug in bzlib.h: stream.next_in should be marked 'const'
189 * but isn't (the library never alters data through the
190 * next_in pointer, only reads it). The result: this ugly
191 * cast to remove 'const'.
193 state->stream.next_in = (char *)(uintptr_t)(const void *)buff;
194 state->stream.avail_in = n;
196 a->compression_read_ahead = read_ahead;
197 a->compression_read_consume = read_consume;
198 a->compression_skip = NULL; /* not supported */
199 a->compression_finish = finish;
201 /* Initialize compression library. */
202 ret = BZ2_bzDecompressInit(&(state->stream),
203 0 /* library verbosity */,
204 0 /* don't use slow low-mem algorithm */);
206 /* If init fails, try using low-memory algorithm instead. */
207 if (ret == BZ_MEM_ERROR) {
208 ret = BZ2_bzDecompressInit(&(state->stream),
209 0 /* library verbosity */,
210 1 /* do use slow low-mem algorithm */);
214 a->compression_data = state;
218 /* Library setup failed: Clean up. */
219 archive_set_error(a, ARCHIVE_ERRNO_MISC,
220 "Internal error initializing %s library", a->compression_name);
221 free(state->uncompressed_buffer);
224 /* Override the error message if we know what really went wrong. */
227 archive_set_error(a, ARCHIVE_ERRNO_MISC,
228 "Internal error initializing compression library: "
229 "invalid setup parameter");
232 archive_set_error(a, ARCHIVE_ERRNO_MISC,
233 "Internal error initializing compression library: "
236 case BZ_CONFIG_ERROR:
237 archive_set_error(a, ARCHIVE_ERRNO_MISC,
238 "Internal error initializing compression library: "
239 "mis-compiled library");
243 return (ARCHIVE_FATAL);
247 * Return a block of data from the decompression buffer. Decompress more
251 read_ahead(struct archive *a, const void **p, size_t min)
253 struct private_data *state;
254 int read_avail, was_avail, ret;
256 state = (struct private_data *)a->compression_data;
258 if (!a->client_reader) {
259 archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
260 "No read callback is registered? "
261 "This is probably an internal programming error.");
262 return (ARCHIVE_FATAL);
265 read_avail = state->stream.next_out - state->read_next;
267 if (read_avail + state->stream.avail_out < min) {
268 memmove(state->uncompressed_buffer, state->read_next,
270 state->read_next = state->uncompressed_buffer;
271 state->stream.next_out = state->read_next + read_avail;
272 state->stream.avail_out
273 = state->uncompressed_buffer_size - read_avail;
276 while (was_avail < read_avail && /* Made some progress. */
277 read_avail < (int)min && /* Haven't satisfied min. */
278 read_avail < (int)state->uncompressed_buffer_size) { /* !full */
279 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
281 was_avail = read_avail;
282 read_avail = state->stream.next_out - state->read_next;
285 *p = state->read_next;
290 * Mark a previously-returned block of data as read.
293 read_consume(struct archive *a, size_t n)
295 struct private_data *state;
297 state = (struct private_data *)a->compression_data;
298 a->file_position += n;
299 state->read_next += n;
300 if (state->read_next > state->stream.next_out)
301 __archive_errx(1, "Request to consume too many "
302 "bytes from bzip2 decompressor");
307 * Clean up the decompressor.
310 finish(struct archive *a)
312 struct private_data *state;
315 state = (struct private_data *)a->compression_data;
317 switch (BZ2_bzDecompressEnd(&(state->stream))) {
321 archive_set_error(a, ARCHIVE_ERRNO_MISC,
322 "Failed to clean up %s compressor", a->compression_name);
326 free(state->uncompressed_buffer);
329 a->compression_data = NULL;
330 if (a->client_closer != NULL)
331 (a->client_closer)(a, a->client_data);
337 * Utility function to pull data through decompressor, reading input
338 * blocks as necessary.
341 drive_decompressor(struct archive *a, struct private_data *state)
344 int decompressed, total_decompressed;
347 total_decompressed = 0;
349 if (state->stream.avail_in == 0) {
350 ret = (a->client_reader)(a, a->client_data,
351 (const void **)&state->stream.next_in);
354 * TODO: Find a better way to handle
359 if (ret == 0 && total_decompressed == 0) {
360 archive_set_error(a, EIO,
361 "Premature end of %s compressed data",
362 a->compression_name);
363 return (ARCHIVE_FATAL);
365 a->raw_position += ret;
366 state->stream.avail_in = ret;
370 output = state->stream.next_out;
372 /* Decompress some data. */
373 ret = BZ2_bzDecompress(&(state->stream));
374 decompressed = state->stream.next_out - output;
376 /* Accumulate the total bytes of output. */
377 state->total_out += decompressed;
378 total_decompressed += decompressed;
381 case BZ_OK: /* Decompressor made some progress. */
382 if (decompressed > 0)
385 case BZ_STREAM_END: /* Found end of stream. */
388 /* Any other return value is an error. */
395 /* Return a fatal error. */
397 archive_set_error(a, ARCHIVE_ERRNO_MISC, "%s decompression failed",
398 a->compression_name);
399 return (ARCHIVE_FATAL);
402 #endif /* HAVE_BZLIB_H */