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: src/lib/libarchive/archive_read_support_compression_bzip2.c,v 1.7 2004/12/22 06:30:14 kientzle Exp $");
41 #include "archive_private.h"
46 unsigned char *uncompressed_buffer
;
47 size_t uncompressed_buffer_size
;
52 static int finish(struct archive
*);
53 static ssize_t
read_ahead(struct archive
*, const void **, size_t);
54 static ssize_t
read_consume(struct archive
*, size_t);
55 static int drive_decompressor(struct archive
*a
, struct private_data
*);
58 /* These two functions are defined even if we lack bzlib. See below. */
59 static int bid(const void *, size_t);
60 static int init(struct archive
*, const void *, size_t);
63 archive_read_support_compression_bzip2(struct archive
*a
)
65 return (__archive_read_register_compression(a
, bid
, init
));
69 * Test whether we can handle this data.
71 * This logic returns zero if any part of the signature fails. It
72 * also tries to Do The Right Thing if a very short buffer prevents us
73 * from verifying as much as we would like.
76 bid(const void *buff
, size_t len
)
78 const unsigned char *buffer
;
86 if (buffer
[0] != 'B') /* Verify first ID byte. */
90 return (bits_checked
);
92 if (buffer
[1] != 'Z') /* Verify second ID byte. */
96 return (bits_checked
);
98 if (buffer
[2] != 'h') /* Verify third ID byte. */
102 return (bits_checked
);
104 if (buffer
[3] < '1' || buffer
[3] > '9')
109 * Research Question: Can we do any more to verify that this
110 * really is BZip2 format?? For 99.9% of the time, the above
111 * test is sufficient, but it would be nice to do a more
112 * thorough check. It's especially troubling that the BZip2
113 * signature begins with all ASCII characters; a tar archive
114 * whose first filename begins with 'BZh3' would potentially
115 * fool this logic. (It may also be possible to gaurd against
116 * such anomalies in archive_read_support_compression_none.)
119 return (bits_checked
);
125 * If we don't have bzlib on this system, we can't actually do the
126 * decompression. We can, however, still detect bzip2-compressed
127 * archives and emit a useful message.
130 init(struct archive
*a
, const void *buff
, size_t n
)
132 (void)a
; /* UNUSED */
133 (void)buff
; /* UNUSED */
134 (void)n
; /* UNUSED */
136 archive_set_error(a
, -1,
137 "This version of libarchive was compiled without bzip2 support");
138 return (ARCHIVE_FATAL
);
145 * Setup the callbacks.
148 init(struct archive
*a
, const void *buff
, size_t n
)
150 struct private_data
*state
;
153 a
->compression_code
= ARCHIVE_COMPRESSION_BZIP2
;
154 a
->compression_name
= "bzip2";
156 state
= malloc(sizeof(*state
));
158 archive_set_error(a
, ENOMEM
,
159 "Can't allocate data for %s decompression",
160 a
->compression_name
);
161 return (ARCHIVE_FATAL
);
163 memset(state
, 0, sizeof(*state
));
165 state
->uncompressed_buffer_size
= 64 * 1024;
166 state
->uncompressed_buffer
= malloc(state
->uncompressed_buffer_size
);
167 state
->stream
.next_out
= state
->uncompressed_buffer
;
168 state
->read_next
= state
->uncompressed_buffer
;
169 state
->stream
.avail_out
= state
->uncompressed_buffer_size
;
171 if (state
->uncompressed_buffer
== NULL
) {
172 archive_set_error(a
, ENOMEM
,
173 "Can't allocate %s decompression buffers",
174 a
->compression_name
);
176 return (ARCHIVE_FATAL
);
180 * A bug in bzlib.h: stream.next_in should be marked 'const'
181 * but isn't (the library never alters data through the
182 * next_in pointer, only reads it). The result: this ugly
183 * cast to remove 'const'.
185 state
->stream
.next_in
= (void *)(uintptr_t)(const void *)buff
;
186 state
->stream
.avail_in
= n
;
188 a
->compression_read_ahead
= read_ahead
;
189 a
->compression_read_consume
= read_consume
;
190 a
->compression_finish
= finish
;
192 /* Initialize compression library. */
193 ret
= BZ2_bzDecompressInit(&(state
->stream
),
194 0 /* library verbosity */,
195 0 /* don't use slow low-mem algorithm */);
197 /* If init fails, try using low-memory algorithm instead. */
198 if (ret
== BZ_MEM_ERROR
) {
199 ret
= BZ2_bzDecompressInit(&(state
->stream
),
200 0 /* library verbosity */,
201 1 /* do use slow low-mem algorithm */);
205 a
->compression_data
= state
;
209 /* Library setup failed: Clean up. */
210 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
211 "Internal error initializing %s library", a
->compression_name
);
212 free(state
->uncompressed_buffer
);
215 /* Override the error message if we know what really went wrong. */
218 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
219 "Internal error initializing compression library: "
220 "invalid setup parameter");
223 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
224 "Internal error initializing compression library: "
227 case BZ_CONFIG_ERROR
:
228 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
229 "Internal error initializing compression library: "
230 "mis-compiled library");
234 return (ARCHIVE_FATAL
);
238 * Return a block of data from the decompression buffer. Decompress more
242 read_ahead(struct archive
*a
, const void **p
, size_t min
)
244 struct private_data
*state
;
245 int read_avail
, was_avail
, ret
;
247 state
= a
->compression_data
;
249 if (!a
->client_reader
) {
250 archive_set_error(a
, ARCHIVE_ERRNO_PROGRAMMER
,
251 "No read callback is registered? "
252 "This is probably an internal programming error.");
253 return (ARCHIVE_FATAL
);
256 read_avail
= state
->stream
.next_out
- state
->read_next
;
258 if (read_avail
+ state
->stream
.avail_out
< min
) {
259 memmove(state
->uncompressed_buffer
, state
->read_next
,
261 state
->read_next
= state
->uncompressed_buffer
;
262 state
->stream
.next_out
= state
->read_next
+ read_avail
;
263 state
->stream
.avail_out
264 = state
->uncompressed_buffer_size
- read_avail
;
267 while (was_avail
< read_avail
&& /* Made some progress. */
268 read_avail
< (int)min
&& /* Haven't satisfied min. */
269 read_avail
< (int)state
->uncompressed_buffer_size
) { /* !full */
270 if ((ret
= drive_decompressor(a
, state
)) != ARCHIVE_OK
)
272 was_avail
= read_avail
;
273 read_avail
= state
->stream
.next_out
- state
->read_next
;
276 *p
= state
->read_next
;
281 * Mark a previously-returned block of data as read.
284 read_consume(struct archive
*a
, size_t n
)
286 struct private_data
*state
;
288 state
= a
->compression_data
;
289 a
->file_position
+= n
;
290 state
->read_next
+= n
;
291 if (state
->read_next
> state
->stream
.next_out
)
292 __archive_errx(1, "Request to consume too many "
293 "bytes from bzip2 decompressor");
298 * Clean up the decompressor.
301 finish(struct archive
*a
)
303 struct private_data
*state
;
306 state
= a
->compression_data
;
308 switch (BZ2_bzDecompressEnd(&(state
->stream
))) {
312 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
313 "Failed to clean up %s compressor", a
->compression_name
);
317 free(state
->uncompressed_buffer
);
320 a
->compression_data
= NULL
;
321 if (a
->client_closer
!= NULL
)
322 (a
->client_closer
)(a
, a
->client_data
);
328 * Utility function to pull data through decompressor, reading input
329 * blocks as necessary.
332 drive_decompressor(struct archive
*a
, struct private_data
*state
)
335 int decompressed
, total_decompressed
;
338 total_decompressed
= 0;
340 if (state
->stream
.avail_in
== 0) {
341 ret
= (a
->client_reader
)(a
, a
->client_data
,
342 (const void **)&state
->stream
.next_in
);
345 * TODO: Find a better way to handle
350 if (ret
== 0 && total_decompressed
== 0) {
351 archive_set_error(a
, EIO
,
352 "Premature end of %s compressed data",
353 a
->compression_name
);
354 return (ARCHIVE_FATAL
);
356 a
->raw_position
+= ret
;
357 state
->stream
.avail_in
= ret
;
361 output
= state
->stream
.next_out
;
363 /* Decompress some data. */
364 ret
= BZ2_bzDecompress(&(state
->stream
));
365 decompressed
= state
->stream
.next_out
- output
;
367 /* Accumulate the total bytes of output. */
368 state
->total_out
+= decompressed
;
369 total_decompressed
+= decompressed
;
372 case BZ_OK
: /* Decompressor made some progress. */
373 if (decompressed
> 0)
376 case BZ_STREAM_END
: /* Found end of stream. */
379 /* Any other return value is an error. */
386 /* Return a fatal error. */
388 archive_set_error(a
, ARCHIVE_ERRNO_MISC
, "%s decompression failed",
389 a
->compression_name
);
390 return (ARCHIVE_FATAL
);
393 #endif /* HAVE_BZLIB_H */