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_gzip.c,v 1.14 2007/04/05 05:18:16 kientzle Exp $");
48 #include "archive_private.h"
49 #include "archive_read_private.h"
54 unsigned char *uncompressed_buffer;
55 size_t uncompressed_buffer_size;
56 unsigned char *read_next;
62 static int finish(struct archive_read *);
63 static ssize_t read_ahead(struct archive_read *, const void **, size_t);
64 static ssize_t read_consume(struct archive_read *, size_t);
65 static int drive_decompressor(struct archive_read *a, struct private_data *);
68 /* These two functions are defined even if we lack zlib. See below. */
69 static int bid(const void *, size_t);
70 static int init(struct archive_read *, const void *, size_t);
73 archive_read_support_compression_gzip(struct archive *_a)
75 struct archive_read *a = (struct archive_read *)_a;
76 if(__archive_read_register_compression(a, bid, init) != NULL)
78 return (ARCHIVE_FATAL);
82 * Test whether we can handle this data.
84 * This logic returns zero if any part of the signature fails. It
85 * also tries to Do The Right Thing if a very short buffer prevents us
86 * from verifying as much as we would like.
89 bid(const void *buff, size_t len)
91 const unsigned char *buffer;
97 buffer = (const unsigned char *)buff;
99 if (buffer[0] != 037) /* Verify first ID byte. */
103 return (bits_checked);
105 if (buffer[1] != 0213) /* Verify second ID byte. */
109 return (bits_checked);
111 if (buffer[2] != 8) /* Compression must be 'deflate'. */
115 return (bits_checked);
117 if ((buffer[3] & 0xE0)!= 0) /* No reserved flags set. */
121 return (bits_checked);
124 * TODO: Verify more; in particular, gzip has an optional
125 * header CRC, which would give us 16 more verified bits. We
126 * may also be able to verify certain constraints on other
130 return (bits_checked);
137 * If we don't have zlib on this system, we can't actually do the
138 * decompression. We can, however, still detect gzip-compressed
139 * archives and emit a useful message.
142 init(struct archive_read *a, const void *buff, size_t n)
144 (void)a; /* UNUSED */
145 (void)buff; /* UNUSED */
146 (void)n; /* UNUSED */
148 archive_set_error(a, -1,
149 "This version of libarchive was compiled without gzip support");
150 return (ARCHIVE_FATAL);
157 * Setup the callbacks.
160 init(struct archive_read *a, const void *buff, size_t n)
162 struct private_data *state;
165 a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
166 a->archive.compression_name = "gzip";
168 state = (struct private_data *)malloc(sizeof(*state));
170 archive_set_error(&a->archive, ENOMEM,
171 "Can't allocate data for %s decompression",
172 a->archive.compression_name);
173 return (ARCHIVE_FATAL);
175 memset(state, 0, sizeof(*state));
177 state->crc = crc32(0L, NULL, 0);
178 state->header_done = 0; /* We've not yet begun to parse header... */
180 state->uncompressed_buffer_size = 64 * 1024;
181 state->uncompressed_buffer = (unsigned char *)malloc(state->uncompressed_buffer_size);
182 state->stream.next_out = state->uncompressed_buffer;
183 state->read_next = state->uncompressed_buffer;
184 state->stream.avail_out = state->uncompressed_buffer_size;
186 if (state->uncompressed_buffer == NULL) {
187 archive_set_error(&a->archive, ENOMEM,
188 "Can't allocate %s decompression buffers",
189 a->archive.compression_name);
191 return (ARCHIVE_FATAL);
195 * A bug in zlib.h: stream.next_in should be marked 'const'
196 * but isn't (the library never alters data through the
197 * next_in pointer, only reads it). The result: this ugly
198 * cast to remove 'const'.
200 state->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff;
201 state->stream.avail_in = n;
203 a->decompressor->read_ahead = read_ahead;
204 a->decompressor->consume = read_consume;
205 a->decompressor->skip = NULL; /* not supported */
206 a->decompressor->finish = finish;
209 * TODO: Do I need to parse the gzip header before calling
210 * inflateInit2()? In particular, one of the header bytes
211 * marks "best compression" or "fastest", which may be
212 * appropriate for setting the second parameter here.
213 * However, I think the only penalty for not setting it
214 * correctly is wasted memory. If this is necessary, it
215 * should probably go into drive_decompressor() below.
218 /* Initialize compression library. */
219 ret = inflateInit2(&(state->stream),
220 -15 /* Don't check for zlib header */);
222 a->decompressor->data = state;
226 /* Library setup failed: Clean up. */
227 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
228 "Internal error initializing %s library",
229 a->archive.compression_name);
230 free(state->uncompressed_buffer);
233 /* Override the error message if we know what really went wrong. */
236 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
237 "Internal error initializing compression library: "
238 "invalid setup parameter");
241 archive_set_error(&a->archive, ENOMEM,
242 "Internal error initializing compression library: "
245 case Z_VERSION_ERROR:
246 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
247 "Internal error initializing compression library: "
248 "invalid library version");
252 return (ARCHIVE_FATAL);
256 * Return a block of data from the decompression buffer. Decompress more
260 read_ahead(struct archive_read *a, const void **p, size_t min)
262 struct private_data *state;
263 size_t read_avail, was_avail;
266 state = (struct private_data *)a->decompressor->data;
267 if (!a->client_reader) {
268 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
269 "No read callback is registered? "
270 "This is probably an internal programming error.");
271 return (ARCHIVE_FATAL);
274 read_avail = state->stream.next_out - state->read_next;
276 if (read_avail + state->stream.avail_out < min) {
277 memmove(state->uncompressed_buffer, state->read_next,
279 state->read_next = state->uncompressed_buffer;
280 state->stream.next_out = state->read_next + read_avail;
281 state->stream.avail_out
282 = state->uncompressed_buffer_size - read_avail;
285 while (read_avail < min && /* Haven't satisfied min. */
286 read_avail < state->uncompressed_buffer_size) { /* !full */
287 was_avail = read_avail;
288 if ((ret = drive_decompressor(a, state)) != ARCHIVE_OK)
290 read_avail = state->stream.next_out - state->read_next;
291 if (was_avail == read_avail) /* No progress? */
295 *p = state->read_next;
300 * Mark a previously-returned block of data as read.
303 read_consume(struct archive_read *a, size_t n)
305 struct private_data *state;
307 state = (struct private_data *)a->decompressor->data;
308 a->archive.file_position += n;
309 state->read_next += n;
310 if (state->read_next > state->stream.next_out)
311 __archive_errx(1, "Request to consume too many "
312 "bytes from gzip decompressor");
317 * Clean up the decompressor.
320 finish(struct archive_read *a)
322 struct private_data *state;
325 state = (struct private_data *)a->decompressor->data;
327 switch (inflateEnd(&(state->stream))) {
331 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
332 "Failed to clean up %s compressor",
333 a->archive.compression_name);
337 free(state->uncompressed_buffer);
340 a->decompressor->data = NULL;
345 * Utility function to pull data through decompressor, reading input
346 * blocks as necessary.
349 drive_decompressor(struct archive_read *a, struct private_data *state)
352 size_t decompressed, total_decompressed;
353 int count, flags, header_state;
354 unsigned char *output;
356 const void *read_buf;
361 total_decompressed = 0;
363 if (state->stream.avail_in == 0) {
364 read_buf = state->stream.next_in;
365 ret = (a->client_reader)(&a->archive, a->client_data,
367 state->stream.next_in = (unsigned char *)(uintptr_t)read_buf;
370 * TODO: Find a better way to handle
375 if (ret == 0 && total_decompressed == 0) {
376 archive_set_error(&a->archive, EIO,
377 "Premature end of %s compressed data",
378 a->archive.compression_name);
379 return (ARCHIVE_FATAL);
381 a->archive.raw_position += ret;
382 state->stream.avail_in = ret;
385 if (!state->header_done) {
387 * If still parsing the header, interpret the
390 b = *(state->stream.next_in++);
391 state->stream.avail_in--;
394 * Yes, this is somewhat crude, but it works,
395 * GZip format isn't likely to change anytime
396 * in the near future, and header parsing is
397 * certainly not a performance issue, so
398 * there's little point in making this more
399 * elegant. Of course, if you see an easy way
400 * to make this more elegant, please let me
403 switch (header_state) {
404 case 0: /* First byte of signature. */
409 case 1: /* Second byte of signature. */
414 case 2: /* Compression type must be 8. */
419 case 3: /* GZip flags. */
423 case 4: case 5: case 6: case 7: /* Mod time. */
426 case 8: /* Deflate flags. */
432 case 10: /* Optional Extra: First byte of Length. */
434 count = 255 & (int)b;
439 * Fall through if there is no
440 * Optional Extra field.
442 case 11: /* Optional Extra: Second byte of Length. */
444 count = (0xff00 & ((int)b << 8)) | count;
449 * Fall through if there is no
450 * Optional Extra field.
452 case 12: /* Optional Extra Field: counted length. */
455 if (count == 0) header_state = 13;
456 else header_state = 12;
460 * Fall through if there is no
461 * Optional Extra field.
463 case 13: /* Optional Original Filename. */
465 if (b == 0) header_state = 14;
466 else header_state = 13;
470 * Fall through if no Optional
473 case 14: /* Optional Comment. */
475 if (b == 0) header_state = 15;
476 else header_state = 14;
479 /* Fall through if no Optional Comment. */
480 case 15: /* Optional Header CRC: First byte. */
485 /* Fall through if no Optional Header CRC. */
486 case 16: /* Optional Header CRC: Second byte. */
491 /* Fall through if no Optional Header CRC. */
492 case 17: /* First byte of compressed data. */
493 state->header_done = 1; /* done with header */
494 state->stream.avail_in++;
495 state->stream.next_in--;
499 * TODO: Consider moving the inflateInit2 call
500 * here so it can include the compression type
504 output = state->stream.next_out;
506 /* Decompress some data. */
507 ret = inflate(&(state->stream), 0);
508 decompressed = state->stream.next_out - output;
510 /* Accumulate the CRC of the uncompressed data. */
511 state->crc = crc32(state->crc, output, decompressed);
513 /* Accumulate the total bytes of output. */
514 state->total_out += decompressed;
515 total_decompressed += decompressed;
518 case Z_OK: /* Decompressor made some progress. */
519 if (decompressed > 0)
522 case Z_STREAM_END: /* Found end of stream. */
524 * TODO: Verify gzip trailer
525 * (uncompressed length and CRC).
529 /* Any other return value is an error. */
530 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
531 "gzip decompression failed (%s)",
539 /* Return a fatal error. */
541 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
542 "%s decompression failed", a->archive.compression_name);
543 return (ARCHIVE_FATAL);
546 #endif /* HAVE_ZLIB_H */