File: gzip.c

package info (click to toggle)
ircii 4.4-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 4,256 kB
  • ctags: 2,797
  • sloc: ansic: 36,743; sh: 907; makefile: 483; lex: 16
file content (277 lines) | stat: -rw-r--r-- 6,669 bytes parent folder | download
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * gzip.c - handle compress, packed, gziped, or lzh (compress -H) files.
 *
 * copyright (c) 1994 matthew green.
 *
 * portions of this file are derived from gzip by jean-loup gailly, and
 * thus this entire file is under the gnu gpl.
 */

/*
 * The unzip code was written and put in the public domain by Mark Adler.
 * Portions of the lzw code are derived from the public domain 'compress'
 * written by Spencer Thomas, Joe Orost, James Woods, Jim McKie, Steve Davies,
 * Ken Turkowski, Dave Mack and Peter Jannesen.
 *
 * See the license_msg below and the file COPYING for the software license.
 */

/*
 * Copyright (C) 1992-1993 Jean-loup Gailly
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef lint
static char rcsid[] = "$Id: gzip.c,v 1.5 1994/12/07 06:17:43 mrg Exp $";
#endif

#include <ctype.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <errno.h>

#include "gzip.h"
#include "lzw.h"

/* configuration */

#if !defined(S_ISDIR) && defined(S_IFDIR)
#  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
#if !defined(S_ISREG) && defined(S_IFREG)
#  define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#endif

/* global buffers */

unsigned char inbuf[INBUFSIZ +INBUF_EXTRA];
unsigned char outbuf[OUTBUFSIZ+OUTBUF_EXTRA];
unsigned short d_buf[DIST_BUFSIZE];
unsigned char window[2L * WSIZE];
unsigned short tab_prefix[1L<<BITS];

/* local variables */

int method = DEFLATED;/* compression method */
int gzip_return_code = OK;   /* program exit code */
int part_nb;          /* number of parts in .gz file */

long bytes_in;             /* number of input bytes */
long bytes_out;            /* number of output bytes */
long total_in = 0;         /* input bytes for all files */
long total_out = 0;        /* output bytes for all files */
struct stat istat;         /* status for input file */
unsigned insize;           /* valid bytes in inbuf */
unsigned inptr;            /* index of next byte to be processed in inbuf */
unsigned outcnt;           /* bytes in output buffer */

/* local functions */

static int	get_method __P((int));
int		(*work) __P((int infile, int outfile)) = zip; /* function to call */

int
zclose(fd)
	int fd;
{
	return close(fd);
}

int
zread(fd, buf, n)
	int fd;
	char *buf;
	int n;
{
	int count = 0;

	if ((*work)(fd, ofd) != OK)
		gzip_return_code = ITZ_WORK_FAILED;
	else
		gzip_return_code = OK;
	return count;
}

int
zopen(path)
	const char *path;
{
	int fd;

	if (stat(path, &istat) != OK)
	{
		gzip_return_code = errno | IGZ_ERRNO;
		return -1;
	}

	if (S_ISDIR(istat.st_mode))
	{
		gzip_return_code = IGZ_ISDIR;
		return -1;
	}
	
	if (!S_ISREG(istat.st_mode))
	{
		gzip_return_code = IGZ_ISNOTREG;
		return -1;
	}

	fd = open(path, O_RDONLY);

	if (fd == -1)
	{
		gzip_return_code = errno | IGZ_ERRNO;
		close(fd);
		return;
	}

	clear_bufs(); /* clear input and output buffers */
	part_nb = 0;

	method = get_method(fd);	/* updates ofname if original given */
	if (method < 0)
	{
		close(fd);
		return -1;		/* error message already emitted */
	}

	return fd;
}


/*
 * Check the magic number of the input file and update ofname if an
 * original name was given and to_stdout is not set.
 * Return the compression method, -1 for error, -2 for warning.
 * Set inptr to the offset of the next byte to be processed.
 * This function may be called repeatedly for an input file consisting
 * of several contiguous gzip'ed members.
 * IN assertions: there is at least one remaining compressed member.
 *   If the member is a zip file, it must be the only one.
 */
static int
get_method(in)
	int in;        /* input file descriptor */
{
	unsigned char flags;
	char magic[2]; /* magic header */

	magic[0] = (char) get_byte();
	magic[1] = (char) get_byte();
	method = -1;                 /* unknown yet */
	part_nb++;                   /* number of parts in gzip file */
	header_bytes = 0;
	/* assume multiple members in gzip file except for record oriented I/O */

	if (memcmp(magic, GZIP_MAGIC, 2) == 0 || memcmp(magic, OLD_GZIP_MAGIC, 2) == 0)
	{

		method = (int) get_byte();
		if (method != DEFLATED)
		{
			gzip_return_code = IGZ_NEED_NEW | IGZ_DEFLATE;
			return -1;
		}
		work = unzip;
		flags = (unsigned char) get_byte();

		if (flags & ENCRYPTED)
		{
			gzip_return_code = IGZ_NEED_NEW | IGZ_ENCRYPT;
			return -1;
		}
		if (flags & CONTINUATION)
		{
			gzip_return_code = IGZ_NEED_NEW | IGZ_CONTIN;
			return -1;
		}
		if (flags & RESERVED)
		{
			gzip_return_code = IGZ_NEED_NEW | IGZ_RESERVED;
			return -1;
		}
		(void) get_byte();	/* ignore timestamp information */
		(void) get_byte();
		(void) get_byte();
		(void) get_byte();

		(void) get_byte();  /* Ignore extra flags for the moment */
		(void) get_byte();  /* Ignore OS type for the moment */

		if (flags & CONTINUATION)
		{
			unsigned part = (unsigned) get_byte();
			part |= ((unsigned) get_byte()) << 8;
		}
		if (flags & EXTRA_FIELD)
		{
			unsigned len = (unsigned) get_byte();
			len |= ((unsigned) get_byte()) << 8;
			while (len--)
				(void) get_byte();
		}

		/* Discard file comment if any */
		if (flags & COMMENT)
			while (get_char())
				;
		if (part_nb == 1)
			header_bytes = inptr + 2 * sizeof(long); /* include crc and size */

	}
	else if (memcmp(magic, PKZIP_MAGIC, 2) == 0 && inptr == 2 && memcmp((char *) inbuf, PKZIP_MAGIC, 4) == 0)
	{
		/* To simplify the code, we support a zip file when alone only.
		 * We are thus guaranteed that the entire local header fits in inbuf.
		 */
		inptr = 0;
		work = unzip;
		if (check_zipfile(in) != OK)
			return -1;
		/* check_zipfile may get ofname from the local header */
	}
	else if (memcmp(magic, PACK_MAGIC, 2) == 0)
	{
		work = unpack;
		method = PACKED;
	}
	else if (memcmp(magic, LZW_MAGIC, 2) == 0)
	{
		work = unlzw;
		method = COMPRESSED;
	}
	else if (memcmp(magic, LZH_MAGIC, 2) == 0)
	{
		work = unlzh;
		method = LZHED;
	}
	else
	{
		method = STORED;
		work = copy;
		inptr = 0;
	}
	if (method >= 0)
		return method;

	if (part_nb == 1)
	{
		gzip_return_code = ERROR;
		return -1;
	}
	else
		return -2;
}