1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27228753Smm
28231200Smm__FBSDID("$FreeBSD$");
29228753Smm
30228753Smm
31228753Smm#ifdef HAVE_ERRNO_H
32228753Smm#include <errno.h>
33228753Smm#endif
34228753Smm#ifdef HAVE_STDLIB_H
35228753Smm#include <stdlib.h>
36228753Smm#endif
37228753Smm#ifdef HAVE_STRING_H
38228753Smm#include <string.h>
39228753Smm#endif
40228753Smm#ifdef HAVE_UNISTD_H
41228753Smm#include <unistd.h>
42228753Smm#endif
43228753Smm#ifdef HAVE_ZLIB_H
44228753Smm#include <zlib.h>
45228753Smm#endif
46228753Smm
47228753Smm#include "archive.h"
48228753Smm#include "archive_private.h"
49228753Smm#include "archive_read_private.h"
50228753Smm
51228753Smm#ifdef HAVE_ZLIB_H
52228753Smmstruct private_data {
53228753Smm	z_stream	 stream;
54228753Smm	char		 in_stream;
55228753Smm	unsigned char	*out_block;
56228753Smm	size_t		 out_block_size;
57228753Smm	int64_t		 total_out;
58228753Smm	unsigned long	 crc;
59228753Smm	char		 eof; /* True = found end of compressed data. */
60228753Smm};
61228753Smm
62228753Smm/* Gzip Filter. */
63228753Smmstatic ssize_t	gzip_filter_read(struct archive_read_filter *, const void **);
64228753Smmstatic int	gzip_filter_close(struct archive_read_filter *);
65228753Smm#endif
66228753Smm
67228753Smm/*
68228753Smm * Note that we can detect gzip archives even if we can't decompress
69228753Smm * them.  (In fact, we like detecting them because we can give better
70228753Smm * error messages.)  So the bid framework here gets compiled even
71228753Smm * if zlib is unavailable.
72228753Smm *
73228753Smm * TODO: If zlib is unavailable, gzip_bidder_init() should
74228753Smm * use the compress_program framework to try to fire up an external
75248616Smm * gzip program.
76228753Smm */
77228753Smmstatic int	gzip_bidder_bid(struct archive_read_filter_bidder *,
78228753Smm		    struct archive_read_filter *);
79228753Smmstatic int	gzip_bidder_init(struct archive_read_filter *);
80228753Smm
81231200Smm#if ARCHIVE_VERSION_NUMBER < 4000000
82231200Smm/* Deprecated; remove in libarchive 4.0 */
83228753Smmint
84231200Smmarchive_read_support_compression_gzip(struct archive *a)
85228753Smm{
86231200Smm	return archive_read_support_filter_gzip(a);
87231200Smm}
88231200Smm#endif
89231200Smm
90231200Smmint
91231200Smmarchive_read_support_filter_gzip(struct archive *_a)
92231200Smm{
93228753Smm	struct archive_read *a = (struct archive_read *)_a;
94231200Smm	struct archive_read_filter_bidder *bidder;
95228753Smm
96231200Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
97231200Smm	    ARCHIVE_STATE_NEW, "archive_read_support_filter_gzip");
98231200Smm
99231200Smm	if (__archive_read_get_bidder(a, &bidder) != ARCHIVE_OK)
100228753Smm		return (ARCHIVE_FATAL);
101228753Smm
102228753Smm	bidder->data = NULL;
103248616Smm	bidder->name = "gzip";
104228753Smm	bidder->bid = gzip_bidder_bid;
105228753Smm	bidder->init = gzip_bidder_init;
106228753Smm	bidder->options = NULL;
107228753Smm	bidder->free = NULL; /* No data, so no cleanup necessary. */
108228753Smm	/* Signal the extent of gzip support with the return value here. */
109228753Smm#if HAVE_ZLIB_H
110228753Smm	return (ARCHIVE_OK);
111228753Smm#else
112228753Smm	archive_set_error(_a, ARCHIVE_ERRNO_MISC,
113248616Smm	    "Using external gzip program");
114228753Smm	return (ARCHIVE_WARN);
115228753Smm#endif
116228753Smm}
117228753Smm
118228753Smm/*
119228753Smm * Read and verify the header.
120228753Smm *
121228753Smm * Returns zero if the header couldn't be validated, else returns
122228753Smm * number of bytes in header.  If pbits is non-NULL, it receives a
123228753Smm * count of bits verified, suitable for use by bidder.
124228753Smm */
125248616Smmstatic ssize_t
126228753Smmpeek_at_header(struct archive_read_filter *filter, int *pbits)
127228753Smm{
128228753Smm	const unsigned char *p;
129228753Smm	ssize_t avail, len;
130228753Smm	int bits = 0;
131228753Smm	int header_flags;
132228753Smm
133228753Smm	/* Start by looking at the first ten bytes of the header, which
134228753Smm	 * is all fixed layout. */
135228753Smm	len = 10;
136228753Smm	p = __archive_read_filter_ahead(filter, len, &avail);
137228753Smm	if (p == NULL || avail == 0)
138228753Smm		return (0);
139231200Smm	/* We only support deflation- third byte must be 0x08. */
140231200Smm	if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
141228753Smm		return (0);
142231200Smm	bits += 24;
143228753Smm	if ((p[3] & 0xE0)!= 0)	/* No reserved flags set. */
144228753Smm		return (0);
145228753Smm	bits += 3;
146228753Smm	header_flags = p[3];
147228753Smm	/* Bytes 4-7 are mod time. */
148228753Smm	/* Byte 8 is deflate flags. */
149228753Smm	/* XXXX TODO: return deflate flags back to consume_header for use
150228753Smm	   in initializing the decompressor. */
151228753Smm	/* Byte 9 is OS. */
152228753Smm
153228753Smm	/* Optional extra data:  2 byte length plus variable body. */
154228753Smm	if (header_flags & 4) {
155228753Smm		p = __archive_read_filter_ahead(filter, len + 2, &avail);
156228753Smm		if (p == NULL)
157228753Smm			return (0);
158228753Smm		len += ((int)p[len + 1] << 8) | (int)p[len];
159228753Smm		len += 2;
160228753Smm	}
161228753Smm
162228753Smm	/* Null-terminated optional filename. */
163228753Smm	if (header_flags & 8) {
164228753Smm		do {
165228753Smm			++len;
166228753Smm			if (avail < len)
167228753Smm				p = __archive_read_filter_ahead(filter,
168228753Smm				    len, &avail);
169228753Smm			if (p == NULL)
170228753Smm				return (0);
171228753Smm		} while (p[len - 1] != 0);
172228753Smm	}
173228753Smm
174228753Smm	/* Null-terminated optional comment. */
175228753Smm	if (header_flags & 16) {
176228753Smm		do {
177228753Smm			++len;
178228753Smm			if (avail < len)
179228753Smm				p = __archive_read_filter_ahead(filter,
180228753Smm				    len, &avail);
181228753Smm			if (p == NULL)
182228753Smm				return (0);
183228753Smm		} while (p[len - 1] != 0);
184228753Smm	}
185228753Smm
186228753Smm	/* Optional header CRC */
187228753Smm	if ((header_flags & 2)) {
188228753Smm		p = __archive_read_filter_ahead(filter, len + 2, &avail);
189228753Smm		if (p == NULL)
190228753Smm			return (0);
191228753Smm#if 0
192228753Smm	int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
193228753Smm	int crc = /* XXX TODO: Compute header CRC. */;
194228753Smm	if (crc != hcrc)
195228753Smm		return (0);
196228753Smm	bits += 16;
197228753Smm#endif
198228753Smm		len += 2;
199228753Smm	}
200228753Smm
201228753Smm	if (pbits != NULL)
202228753Smm		*pbits = bits;
203228753Smm	return (len);
204228753Smm}
205228753Smm
206228753Smm/*
207228753Smm * Bidder just verifies the header and returns the number of verified bits.
208228753Smm */
209228753Smmstatic int
210228753Smmgzip_bidder_bid(struct archive_read_filter_bidder *self,
211228753Smm    struct archive_read_filter *filter)
212228753Smm{
213228753Smm	int bits_checked;
214228753Smm
215228753Smm	(void)self; /* UNUSED */
216228753Smm
217228753Smm	if (peek_at_header(filter, &bits_checked))
218228753Smm		return (bits_checked);
219228753Smm	return (0);
220228753Smm}
221228753Smm
222228753Smm
223228753Smm#ifndef HAVE_ZLIB_H
224228753Smm
225228753Smm/*
226228753Smm * If we don't have the library on this system, we can't do the
227248616Smm * decompression directly.  We can, however, try to run "gzip -d"
228228753Smm * in case that's available.
229228753Smm */
230228753Smmstatic int
231228753Smmgzip_bidder_init(struct archive_read_filter *self)
232228753Smm{
233228753Smm	int r;
234228753Smm
235248616Smm	r = __archive_read_program(self, "gzip -d");
236228753Smm	/* Note: We set the format here even if __archive_read_program()
237228753Smm	 * above fails.  We do, after all, know what the format is
238228753Smm	 * even if we weren't able to read it. */
239248616Smm	self->code = ARCHIVE_FILTER_GZIP;
240228753Smm	self->name = "gzip";
241228753Smm	return (r);
242228753Smm}
243228753Smm
244228753Smm#else
245228753Smm
246228753Smm/*
247228753Smm * Initialize the filter object.
248228753Smm */
249228753Smmstatic int
250228753Smmgzip_bidder_init(struct archive_read_filter *self)
251228753Smm{
252228753Smm	struct private_data *state;
253228753Smm	static const size_t out_block_size = 64 * 1024;
254228753Smm	void *out_block;
255228753Smm
256248616Smm	self->code = ARCHIVE_FILTER_GZIP;
257228753Smm	self->name = "gzip";
258228753Smm
259228753Smm	state = (struct private_data *)calloc(sizeof(*state), 1);
260228753Smm	out_block = (unsigned char *)malloc(out_block_size);
261228753Smm	if (state == NULL || out_block == NULL) {
262228753Smm		free(out_block);
263228753Smm		free(state);
264228753Smm		archive_set_error(&self->archive->archive, ENOMEM,
265228753Smm		    "Can't allocate data for gzip decompression");
266228753Smm		return (ARCHIVE_FATAL);
267228753Smm	}
268228753Smm
269228753Smm	self->data = state;
270228753Smm	state->out_block_size = out_block_size;
271228753Smm	state->out_block = out_block;
272228753Smm	self->read = gzip_filter_read;
273228753Smm	self->skip = NULL; /* not supported */
274228753Smm	self->close = gzip_filter_close;
275228753Smm
276228753Smm	state->in_stream = 0; /* We're not actually within a stream yet. */
277228753Smm
278228753Smm	return (ARCHIVE_OK);
279228753Smm}
280228753Smm
281228753Smmstatic int
282228753Smmconsume_header(struct archive_read_filter *self)
283228753Smm{
284228753Smm	struct private_data *state;
285228753Smm	ssize_t avail;
286228753Smm	size_t len;
287228753Smm	int ret;
288228753Smm
289228753Smm	state = (struct private_data *)self->data;
290228753Smm
291228753Smm	/* If this is a real header, consume it. */
292228753Smm	len = peek_at_header(self->upstream, NULL);
293228753Smm	if (len == 0)
294228753Smm		return (ARCHIVE_EOF);
295228753Smm	__archive_read_filter_consume(self->upstream, len);
296228753Smm
297228753Smm	/* Initialize CRC accumulator. */
298228753Smm	state->crc = crc32(0L, NULL, 0);
299228753Smm
300228753Smm	/* Initialize compression library. */
301228753Smm	state->stream.next_in = (unsigned char *)(uintptr_t)
302228753Smm	    __archive_read_filter_ahead(self->upstream, 1, &avail);
303248616Smm	state->stream.avail_in = (uInt)avail;
304228753Smm	ret = inflateInit2(&(state->stream),
305228753Smm	    -15 /* Don't check for zlib header */);
306228753Smm
307228753Smm	/* Decipher the error code. */
308228753Smm	switch (ret) {
309228753Smm	case Z_OK:
310228753Smm		state->in_stream = 1;
311228753Smm		return (ARCHIVE_OK);
312228753Smm	case Z_STREAM_ERROR:
313228753Smm		archive_set_error(&self->archive->archive,
314228753Smm		    ARCHIVE_ERRNO_MISC,
315228753Smm		    "Internal error initializing compression library: "
316228753Smm		    "invalid setup parameter");
317228753Smm		break;
318228753Smm	case Z_MEM_ERROR:
319228753Smm		archive_set_error(&self->archive->archive, ENOMEM,
320228753Smm		    "Internal error initializing compression library: "
321228753Smm		    "out of memory");
322228753Smm		break;
323228753Smm	case Z_VERSION_ERROR:
324228753Smm		archive_set_error(&self->archive->archive,
325228753Smm		    ARCHIVE_ERRNO_MISC,
326228753Smm		    "Internal error initializing compression library: "
327228753Smm		    "invalid library version");
328228753Smm		break;
329228753Smm	default:
330228753Smm		archive_set_error(&self->archive->archive,
331228753Smm		    ARCHIVE_ERRNO_MISC,
332228753Smm		    "Internal error initializing compression library: "
333228753Smm		    " Zlib error %d", ret);
334228753Smm		break;
335228753Smm	}
336228753Smm	return (ARCHIVE_FATAL);
337228753Smm}
338228753Smm
339228753Smmstatic int
340228753Smmconsume_trailer(struct archive_read_filter *self)
341228753Smm{
342228753Smm	struct private_data *state;
343228753Smm	const unsigned char *p;
344228753Smm	ssize_t avail;
345228753Smm
346228753Smm	state = (struct private_data *)self->data;
347228753Smm
348228753Smm	state->in_stream = 0;
349228753Smm	switch (inflateEnd(&(state->stream))) {
350228753Smm	case Z_OK:
351228753Smm		break;
352228753Smm	default:
353228753Smm		archive_set_error(&self->archive->archive,
354228753Smm		    ARCHIVE_ERRNO_MISC,
355228753Smm		    "Failed to clean up gzip decompressor");
356228753Smm		return (ARCHIVE_FATAL);
357228753Smm	}
358228753Smm
359228753Smm	/* GZip trailer is a fixed 8 byte structure. */
360228753Smm	p = __archive_read_filter_ahead(self->upstream, 8, &avail);
361228753Smm	if (p == NULL || avail == 0)
362228753Smm		return (ARCHIVE_FATAL);
363228753Smm
364228753Smm	/* XXX TODO: Verify the length and CRC. */
365228753Smm
366228753Smm	/* We've verified the trailer, so consume it now. */
367228753Smm	__archive_read_filter_consume(self->upstream, 8);
368228753Smm
369228753Smm	return (ARCHIVE_OK);
370228753Smm}
371228753Smm
372228753Smmstatic ssize_t
373228753Smmgzip_filter_read(struct archive_read_filter *self, const void **p)
374228753Smm{
375228753Smm	struct private_data *state;
376228753Smm	size_t decompressed;
377228753Smm	ssize_t avail_in;
378228753Smm	int ret;
379228753Smm
380228753Smm	state = (struct private_data *)self->data;
381228753Smm
382228753Smm	/* Empty our output buffer. */
383228753Smm	state->stream.next_out = state->out_block;
384248616Smm	state->stream.avail_out = (uInt)state->out_block_size;
385228753Smm
386228753Smm	/* Try to fill the output buffer. */
387228753Smm	while (state->stream.avail_out > 0 && !state->eof) {
388228753Smm		/* If we're not in a stream, read a header
389228753Smm		 * and initialize the decompression library. */
390228753Smm		if (!state->in_stream) {
391228753Smm			ret = consume_header(self);
392228753Smm			if (ret == ARCHIVE_EOF) {
393228753Smm				state->eof = 1;
394228753Smm				break;
395228753Smm			}
396228753Smm			if (ret < ARCHIVE_OK)
397228753Smm				return (ret);
398228753Smm		}
399228753Smm
400228753Smm		/* Peek at the next available data. */
401228753Smm		/* ZLib treats stream.next_in as const but doesn't declare
402228753Smm		 * it so, hence this ugly cast. */
403228753Smm		state->stream.next_in = (unsigned char *)(uintptr_t)
404228753Smm		    __archive_read_filter_ahead(self->upstream, 1, &avail_in);
405231200Smm		if (state->stream.next_in == NULL) {
406231200Smm			archive_set_error(&self->archive->archive,
407231200Smm			    ARCHIVE_ERRNO_MISC,
408231200Smm			    "truncated gzip input");
409228753Smm			return (ARCHIVE_FATAL);
410231200Smm		}
411248616Smm		state->stream.avail_in = (uInt)avail_in;
412228753Smm
413228753Smm		/* Decompress and consume some of that data. */
414228753Smm		ret = inflate(&(state->stream), 0);
415228753Smm		switch (ret) {
416228753Smm		case Z_OK: /* Decompressor made some progress. */
417228753Smm			__archive_read_filter_consume(self->upstream,
418228753Smm			    avail_in - state->stream.avail_in);
419228753Smm			break;
420228753Smm		case Z_STREAM_END: /* Found end of stream. */
421228753Smm			__archive_read_filter_consume(self->upstream,
422228753Smm			    avail_in - state->stream.avail_in);
423228753Smm			/* Consume the stream trailer; release the
424228753Smm			 * decompression library. */
425228753Smm			ret = consume_trailer(self);
426228753Smm			if (ret < ARCHIVE_OK)
427228753Smm				return (ret);
428228753Smm			break;
429228753Smm		default:
430228753Smm			/* Return an error. */
431228753Smm			archive_set_error(&self->archive->archive,
432228753Smm			    ARCHIVE_ERRNO_MISC,
433228753Smm			    "gzip decompression failed");
434228753Smm			return (ARCHIVE_FATAL);
435228753Smm		}
436228753Smm	}
437228753Smm
438228753Smm	/* We've read as much as we can. */
439228753Smm	decompressed = state->stream.next_out - state->out_block;
440228753Smm	state->total_out += decompressed;
441228753Smm	if (decompressed == 0)
442228753Smm		*p = NULL;
443228753Smm	else
444228753Smm		*p = state->out_block;
445228753Smm	return (decompressed);
446228753Smm}
447228753Smm
448228753Smm/*
449228753Smm * Clean up the decompressor.
450228753Smm */
451228753Smmstatic int
452228753Smmgzip_filter_close(struct archive_read_filter *self)
453228753Smm{
454228753Smm	struct private_data *state;
455228753Smm	int ret;
456228753Smm
457228753Smm	state = (struct private_data *)self->data;
458228753Smm	ret = ARCHIVE_OK;
459228753Smm
460228753Smm	if (state->in_stream) {
461228753Smm		switch (inflateEnd(&(state->stream))) {
462228753Smm		case Z_OK:
463228753Smm			break;
464228753Smm		default:
465228753Smm			archive_set_error(&(self->archive->archive),
466228753Smm			    ARCHIVE_ERRNO_MISC,
467228753Smm			    "Failed to clean up gzip compressor");
468228753Smm			ret = ARCHIVE_FATAL;
469228753Smm		}
470228753Smm	}
471228753Smm
472228753Smm	free(state->out_block);
473228753Smm	free(state);
474228753Smm	return (ret);
475228753Smm}
476228753Smm
477228753Smm#endif /* HAVE_ZLIB_H */
478