summaryrefslogtreecommitdiff
path: root/bin/gzip/unzip.c
blob: ba259afa529419ff52125b6892aeb093f8dbc455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* unzip.c -- decompress files in gzip or pkzip format.
 * Copyright (C) 1992-1993 Jean-loup Gailly
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License, see the file COPYING.
 *
 * The code in this file is derived from the file funzip.c written
 * and put in the public domain by Mark Adler.
 */

/*
   This version can extract files in gzip format.
   Only the first entry is extracted, and it has to be
   either deflated or stored.
 */

#ifdef RCSID
static char rcsid[] = "$Id: unzip.c,v 1.1 2002/08/18 00:59:21 hpa Exp $";
#endif

#include "tailor.h"
#include "gzip.h"

/* ===========================================================================
 * Unzip in to out.  This routine works on gzip files only.
 *
 * IN assertions: the buffer inbuf contains already the beginning of
 *   the compressed data, from offsets inptr to insize-1 included.
 *   The magic header has already been checked. The output buffer is cleared.
 */
int unzip(int in, int out)
{
	ulg orig_crc = 0; /* original crc */
	ulg orig_len = 0; /* original uncompressed length */
	int n;
	uch buf[8]; /* extended local header */

	ifd = in;
	ofd = out;

	updcrc(NULL, 0); /* initialize crc */

	/* Decompress */
	if (method == DEFLATED) {
		int res = inflate();

		if (res == 3) {
			error("out of memory");
		} else if (res != 0) {
			error("invalid compressed data--format violated");
		}

	} else {
		error("internal error, invalid method");
	}

	/* Get the crc and original length */
	/* crc32  (see algorithm.doc)
	 * uncompressed input size modulo 2^32
	 */
	for (n = 0; n < 8; n++) {
		buf[n] = (uch)get_byte(); /* may cause an error if EOF */
	}
	orig_crc = LG(buf);
	orig_len = LG(buf + 4);

	/* Validate decompression */
	if (orig_crc != updcrc(outbuf, 0)) {
		error("invalid compressed data--crc error");
	}
	if (orig_len != (ulg)bytes_out) {
		error("invalid compressed data--length error");
	}

	return OK;
}