Oops, add missing space.
[dragonfly/port-amd64.git] / contrib / libarchive-2 / libarchive / archive_read_support_compression_gzip.c
blobb2e0d8bd95936c588caeb90659a62b4f80d26ead
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 $");
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #ifdef HAVE_ZLIB_H
44 #include <zlib.h>
45 #endif
47 #include "archive.h"
48 #include "archive_private.h"
49 #include "archive_read_private.h"
51 #ifdef HAVE_ZLIB_H
52 struct private_data {
53 z_stream stream;
54 unsigned char *uncompressed_buffer;
55 size_t uncompressed_buffer_size;
56 unsigned char *read_next;
57 int64_t total_out;
58 unsigned long crc;
59 char header_done;
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 *);
67 #endif
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);
73 int
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)
78 return (ARCHIVE_OK);
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.
89 static int
90 bid(const void *buff, size_t len)
92 const unsigned char *buffer;
93 int bits_checked;
95 if (len < 1)
96 return (0);
98 buffer = (const unsigned char *)buff;
99 bits_checked = 0;
100 if (buffer[0] != 037) /* Verify first ID byte. */
101 return (0);
102 bits_checked += 8;
103 if (len < 2)
104 return (bits_checked);
106 if (buffer[1] != 0213) /* Verify second ID byte. */
107 return (0);
108 bits_checked += 8;
109 if (len < 3)
110 return (bits_checked);
112 if (buffer[2] != 8) /* Compression must be 'deflate'. */
113 return (0);
114 bits_checked += 8;
115 if (len < 4)
116 return (bits_checked);
118 if ((buffer[3] & 0xE0)!= 0) /* No reserved flags set. */
119 return (0);
120 bits_checked += 3;
121 if (len < 5)
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
128 * fields.
131 return (bits_checked);
135 #ifndef HAVE_ZLIB_H
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.
142 static int
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, -1,
150 "This version of libarchive was compiled without gzip support");
151 return (ARCHIVE_FATAL);
155 #else
158 * Setup the callbacks.
160 static int
161 init(struct archive_read *a, const void *buff, size_t n)
163 struct private_data *state;
164 int ret;
166 a->archive.compression_code = ARCHIVE_COMPRESSION_GZIP;
167 a->archive.compression_name = "gzip";
169 state = (struct private_data *)malloc(sizeof(*state));
170 if (state == NULL) {
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);
191 free(state);
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 */);
222 if (ret == Z_OK) {
223 a->decompressor->data = state;
224 return (ARCHIVE_OK);
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);
232 free(state);
234 /* Override the error message if we know what really went wrong. */
235 switch (ret) {
236 case Z_STREAM_ERROR:
237 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
238 "Internal error initializing compression library: "
239 "invalid setup parameter");
240 break;
241 case Z_MEM_ERROR:
242 archive_set_error(&a->archive, ENOMEM,
243 "Internal error initializing compression library: "
244 "out of memory");
245 break;
246 case Z_VERSION_ERROR:
247 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
248 "Internal error initializing compression library: "
249 "invalid library version");
250 break;
253 return (ARCHIVE_FATAL);
257 * Return a block of data from the decompression buffer. Decompress more
258 * as necessary.
260 static ssize_t
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;
265 int ret;
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,
279 read_avail);
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)
290 return (ret);
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? */
295 break;
298 *p = state->read_next;
299 return (read_avail);
303 * Mark a previously-returned block of data as read.
305 static ssize_t
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");
316 return (n);
320 * Clean up the decompressor.
322 static int
323 finish(struct archive_read *a)
325 struct private_data *state;
326 int ret;
328 state = (struct private_data *)a->decompressor->data;
329 ret = ARCHIVE_OK;
330 switch (inflateEnd(&(state->stream))) {
331 case Z_OK:
332 break;
333 default:
334 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
335 "Failed to clean up %s compressor",
336 a->archive.compression_name);
337 ret = ARCHIVE_FATAL;
340 free(state->uncompressed_buffer);
341 free(state);
343 a->decompressor->data = NULL;
344 return (ret);
348 * Utility function to pull data through decompressor, reading input
349 * blocks as necessary.
351 static int
352 drive_decompressor(struct archive_read *a, struct private_data *state)
354 ssize_t ret;
355 size_t decompressed, total_decompressed;
356 int count, flags, header_state;
357 unsigned char *output;
358 unsigned char b;
359 const void *read_buf;
361 if (state->eof)
362 return (ARCHIVE_EOF);
363 flags = 0;
364 count = 0;
365 header_state = 0;
366 total_decompressed = 0;
367 for (;;) {
368 if (state->stream.avail_in == 0) {
369 read_buf = state->stream.next_in;
370 ret = (a->client_reader)(&a->archive, a->client_data,
371 &read_buf);
372 state->stream.next_in = (unsigned char *)(uintptr_t)read_buf;
373 if (ret < 0) {
375 * TODO: Find a better way to handle
376 * this read failure.
378 goto fatal;
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
393 * next byte.
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
406 * know.. ;-)
408 switch (header_state) {
409 case 0: /* First byte of signature. */
410 if (b != 037)
411 goto fatal;
412 header_state = 1;
413 break;
414 case 1: /* Second byte of signature. */
415 if (b != 0213)
416 goto fatal;
417 header_state = 2;
418 break;
419 case 2: /* Compression type must be 8. */
420 if (b != 8)
421 goto fatal;
422 header_state = 3;
423 break;
424 case 3: /* GZip flags. */
425 flags = b;
426 header_state = 4;
427 break;
428 case 4: case 5: case 6: case 7: /* Mod time. */
429 header_state++;
430 break;
431 case 8: /* Deflate flags. */
432 header_state = 9;
433 break;
434 case 9: /* OS. */
435 header_state = 10;
436 break;
437 case 10: /* Optional Extra: First byte of Length. */
438 if ((flags & 4)) {
439 count = 255 & (int)b;
440 header_state = 11;
441 break;
444 * Fall through if there is no
445 * Optional Extra field.
447 case 11: /* Optional Extra: Second byte of Length. */
448 if ((flags & 4)) {
449 count = (0xff00 & ((int)b << 8)) | count;
450 header_state = 12;
451 break;
454 * Fall through if there is no
455 * Optional Extra field.
457 case 12: /* Optional Extra Field: counted length. */
458 if ((flags & 4)) {
459 --count;
460 if (count == 0) header_state = 13;
461 else header_state = 12;
462 break;
465 * Fall through if there is no
466 * Optional Extra field.
468 case 13: /* Optional Original Filename. */
469 if ((flags & 8)) {
470 if (b == 0) header_state = 14;
471 else header_state = 13;
472 break;
475 * Fall through if no Optional
476 * Original Filename.
478 case 14: /* Optional Comment. */
479 if ((flags & 16)) {
480 if (b == 0) header_state = 15;
481 else header_state = 14;
482 break;
484 /* Fall through if no Optional Comment. */
485 case 15: /* Optional Header CRC: First byte. */
486 if ((flags & 2)) {
487 header_state = 16;
488 break;
490 /* Fall through if no Optional Header CRC. */
491 case 16: /* Optional Header CRC: Second byte. */
492 if ((flags & 2)) {
493 header_state = 17;
494 break;
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
506 * from the header?
508 } else {
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;
522 switch (ret) {
523 case Z_OK: /* Decompressor made some progress. */
524 if (decompressed > 0)
525 return (ARCHIVE_OK);
526 break;
527 case Z_STREAM_END: /* Found end of stream. */
529 * TODO: Verify gzip trailer
530 * (uncompressed length and CRC).
532 state->eof = 1;
533 return (ARCHIVE_OK);
534 default:
535 /* Any other return value is an error. */
536 goto fatal;
540 return (ARCHIVE_OK);
542 /* Return a fatal error. */
543 fatal:
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 */