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.16 2008/02/19 05:44:59 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
;
60 char eof
; /* True = found end of compressed data. */
63 static int finish(struct archive_read
*);
64 static ssize_t
read_ahead(struct archive_read
*, const void **, size_t);
65 static ssize_t
read_consume(struct archive_read
*, size_t);
66 static int drive_decompressor(struct archive_read
*a
, struct private_data
*);
69 /* These two functions are defined even if we lack the library. See below. */
70 static int bid(const void *, size_t);
71 static int init(struct archive_read
*, const void *, size_t);
74 archive_read_support_compression_gzip(struct archive
*_a
)
76 struct archive_read
*a
= (struct archive_read
*)_a
;
77 if (__archive_read_register_compression(a
, bid
, init
) != NULL
)
79 return (ARCHIVE_FATAL
);
83 * Test whether we can handle this data.
85 * This logic returns zero if any part of the signature fails. It
86 * also tries to Do The Right Thing if a very short buffer prevents us
87 * from verifying as much as we would like.
90 bid(const void *buff
, size_t len
)
92 const unsigned char *buffer
;
98 buffer
= (const unsigned char *)buff
;
100 if (buffer
[0] != 037) /* Verify first ID byte. */
104 return (bits_checked
);
106 if (buffer
[1] != 0213) /* Verify second ID byte. */
110 return (bits_checked
);
112 if (buffer
[2] != 8) /* Compression must be 'deflate'. */
116 return (bits_checked
);
118 if ((buffer
[3] & 0xE0)!= 0) /* No reserved flags set. */
122 return (bits_checked
);
125 * TODO: Verify more; in particular, gzip has an optional
126 * header CRC, which would give us 16 more verified bits. We
127 * may also be able to verify certain constraints on other
131 return (bits_checked
);
138 * If we don't have the library on this system, we can't actually do the
139 * decompression. We can, however, still detect compressed archives
140 * and emit a useful message.
143 init(struct archive_read
*a
, const void *buff
, size_t n
)
145 (void)a
; /* UNUSED */
146 (void)buff
; /* UNUSED */
147 (void)n
; /* UNUSED */
149 archive_set_error(&a
->archive
, -1,
150 "This version of libarchive was compiled without gzip support");
151 return (ARCHIVE_FATAL
);
158 * Setup the callbacks.
161 init(struct archive_read
*a
, const void *buff
, size_t n
)
163 struct private_data
*state
;
166 a
->archive
.compression_code
= ARCHIVE_COMPRESSION_GZIP
;
167 a
->archive
.compression_name
= "gzip";
169 state
= (struct private_data
*)malloc(sizeof(*state
));
171 archive_set_error(&a
->archive
, ENOMEM
,
172 "Can't allocate data for %s decompression",
173 a
->archive
.compression_name
);
174 return (ARCHIVE_FATAL
);
176 memset(state
, 0, sizeof(*state
));
178 state
->crc
= crc32(0L, NULL
, 0);
179 state
->header_done
= 0; /* We've not yet begun to parse header... */
181 state
->uncompressed_buffer_size
= 64 * 1024;
182 state
->uncompressed_buffer
= (unsigned char *)malloc(state
->uncompressed_buffer_size
);
183 state
->stream
.next_out
= state
->uncompressed_buffer
;
184 state
->read_next
= state
->uncompressed_buffer
;
185 state
->stream
.avail_out
= state
->uncompressed_buffer_size
;
187 if (state
->uncompressed_buffer
== NULL
) {
188 archive_set_error(&a
->archive
, ENOMEM
,
189 "Can't allocate %s decompression buffers",
190 a
->archive
.compression_name
);
192 return (ARCHIVE_FATAL
);
196 * A bug in zlib.h: stream.next_in should be marked 'const'
197 * but isn't (the library never alters data through the
198 * next_in pointer, only reads it). The result: this ugly
199 * cast to remove 'const'.
201 state
->stream
.next_in
= (Bytef
*)(uintptr_t)(const void *)buff
;
202 state
->stream
.avail_in
= n
;
204 a
->decompressor
->read_ahead
= read_ahead
;
205 a
->decompressor
->consume
= read_consume
;
206 a
->decompressor
->skip
= NULL
; /* not supported */
207 a
->decompressor
->finish
= finish
;
210 * TODO: Do I need to parse the gzip header before calling
211 * inflateInit2()? In particular, one of the header bytes
212 * marks "best compression" or "fastest", which may be
213 * appropriate for setting the second parameter here.
214 * However, I think the only penalty for not setting it
215 * correctly is wasted memory. If this is necessary, it
216 * should probably go into drive_decompressor() below.
219 /* Initialize compression library. */
220 ret
= inflateInit2(&(state
->stream
),
221 -15 /* Don't check for zlib header */);
223 a
->decompressor
->data
= state
;
227 /* Library setup failed: Clean up. */
228 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
229 "Internal error initializing %s library",
230 a
->archive
.compression_name
);
231 free(state
->uncompressed_buffer
);
234 /* Override the error message if we know what really went wrong. */
237 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
238 "Internal error initializing compression library: "
239 "invalid setup parameter");
242 archive_set_error(&a
->archive
, ENOMEM
,
243 "Internal error initializing compression library: "
246 case Z_VERSION_ERROR
:
247 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
248 "Internal error initializing compression library: "
249 "invalid library version");
253 return (ARCHIVE_FATAL
);
257 * Return a block of data from the decompression buffer. Decompress more
261 read_ahead(struct archive_read
*a
, const void **p
, size_t min
)
263 struct private_data
*state
;
264 size_t read_avail
, was_avail
;
267 state
= (struct private_data
*)a
->decompressor
->data
;
268 if (!a
->client_reader
) {
269 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_PROGRAMMER
,
270 "No read callback is registered? "
271 "This is probably an internal programming error.");
272 return (ARCHIVE_FATAL
);
275 read_avail
= state
->stream
.next_out
- state
->read_next
;
277 if (read_avail
+ state
->stream
.avail_out
< min
) {
278 memmove(state
->uncompressed_buffer
, state
->read_next
,
280 state
->read_next
= state
->uncompressed_buffer
;
281 state
->stream
.next_out
= state
->read_next
+ read_avail
;
282 state
->stream
.avail_out
283 = state
->uncompressed_buffer_size
- read_avail
;
286 while (read_avail
< min
&& /* Haven't satisfied min. */
287 read_avail
< state
->uncompressed_buffer_size
) { /* !full */
288 was_avail
= read_avail
;
289 if ((ret
= drive_decompressor(a
, state
)) < ARCHIVE_OK
)
291 if (ret
== ARCHIVE_EOF
)
292 break; /* Break on EOF even if we haven't met min. */
293 read_avail
= state
->stream
.next_out
- state
->read_next
;
294 if (was_avail
== read_avail
) /* No progress? */
298 *p
= state
->read_next
;
303 * Mark a previously-returned block of data as read.
306 read_consume(struct archive_read
*a
, size_t n
)
308 struct private_data
*state
;
310 state
= (struct private_data
*)a
->decompressor
->data
;
311 a
->archive
.file_position
+= n
;
312 state
->read_next
+= n
;
313 if (state
->read_next
> state
->stream
.next_out
)
314 __archive_errx(1, "Request to consume too many "
315 "bytes from gzip decompressor");
320 * Clean up the decompressor.
323 finish(struct archive_read
*a
)
325 struct private_data
*state
;
328 state
= (struct private_data
*)a
->decompressor
->data
;
330 switch (inflateEnd(&(state
->stream
))) {
334 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
335 "Failed to clean up %s compressor",
336 a
->archive
.compression_name
);
340 free(state
->uncompressed_buffer
);
343 a
->decompressor
->data
= NULL
;
348 * Utility function to pull data through decompressor, reading input
349 * blocks as necessary.
352 drive_decompressor(struct archive_read
*a
, struct private_data
*state
)
355 size_t decompressed
, total_decompressed
;
356 int count
, flags
, header_state
;
357 unsigned char *output
;
359 const void *read_buf
;
362 return (ARCHIVE_EOF
);
366 total_decompressed
= 0;
368 if (state
->stream
.avail_in
== 0) {
369 read_buf
= state
->stream
.next_in
;
370 ret
= (a
->client_reader
)(&a
->archive
, a
->client_data
,
372 state
->stream
.next_in
= (unsigned char *)(uintptr_t)read_buf
;
375 * TODO: Find a better way to handle
380 if (ret
== 0 && total_decompressed
== 0) {
381 archive_set_error(&a
->archive
, EIO
,
382 "Premature end of %s compressed data",
383 a
->archive
.compression_name
);
384 return (ARCHIVE_FATAL
);
386 a
->archive
.raw_position
+= ret
;
387 state
->stream
.avail_in
= ret
;
390 if (!state
->header_done
) {
392 * If still parsing the header, interpret the
395 b
= *(state
->stream
.next_in
++);
396 state
->stream
.avail_in
--;
399 * Yes, this is somewhat crude, but it works,
400 * GZip format isn't likely to change anytime
401 * in the near future, and header parsing is
402 * certainly not a performance issue, so
403 * there's little point in making this more
404 * elegant. Of course, if you see an easy way
405 * to make this more elegant, please let me
408 switch (header_state
) {
409 case 0: /* First byte of signature. */
414 case 1: /* Second byte of signature. */
419 case 2: /* Compression type must be 8. */
424 case 3: /* GZip flags. */
428 case 4: case 5: case 6: case 7: /* Mod time. */
431 case 8: /* Deflate flags. */
437 case 10: /* Optional Extra: First byte of Length. */
439 count
= 255 & (int)b
;
444 * Fall through if there is no
445 * Optional Extra field.
447 case 11: /* Optional Extra: Second byte of Length. */
449 count
= (0xff00 & ((int)b
<< 8)) | count
;
454 * Fall through if there is no
455 * Optional Extra field.
457 case 12: /* Optional Extra Field: counted length. */
460 if (count
== 0) header_state
= 13;
461 else header_state
= 12;
465 * Fall through if there is no
466 * Optional Extra field.
468 case 13: /* Optional Original Filename. */
470 if (b
== 0) header_state
= 14;
471 else header_state
= 13;
475 * Fall through if no Optional
478 case 14: /* Optional Comment. */
480 if (b
== 0) header_state
= 15;
481 else header_state
= 14;
484 /* Fall through if no Optional Comment. */
485 case 15: /* Optional Header CRC: First byte. */
490 /* Fall through if no Optional Header CRC. */
491 case 16: /* Optional Header CRC: Second byte. */
496 /* Fall through if no Optional Header CRC. */
497 case 17: /* First byte of compressed data. */
498 state
->header_done
= 1; /* done with header */
499 state
->stream
.avail_in
++;
500 state
->stream
.next_in
--;
504 * TODO: Consider moving the inflateInit2 call
505 * here so it can include the compression type
509 output
= state
->stream
.next_out
;
511 /* Decompress some data. */
512 ret
= inflate(&(state
->stream
), 0);
513 decompressed
= state
->stream
.next_out
- output
;
515 /* Accumulate the CRC of the uncompressed data. */
516 state
->crc
= crc32(state
->crc
, output
, decompressed
);
518 /* Accumulate the total bytes of output. */
519 state
->total_out
+= decompressed
;
520 total_decompressed
+= decompressed
;
523 case Z_OK
: /* Decompressor made some progress. */
524 if (decompressed
> 0)
527 case Z_STREAM_END
: /* Found end of stream. */
529 * TODO: Verify gzip trailer
530 * (uncompressed length and CRC).
535 /* Any other return value is an error. */
542 /* Return a fatal error. */
544 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
545 "%s decompression failed", a
->archive
.compression_name
);
546 return (ARCHIVE_FATAL
);
549 #endif /* HAVE_ZLIB_H */