xref: /illumos-gate/usr/src/boot/libsa/gzipfs.c (revision b9a2a14b8f156d14859ac7ceeea07ac7194df08d)
1 /*
2  * Copyright (c) 1998 Michael Smith.
3  * All rights reserved.
4  *
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.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 
29 #include "stand.h"
30 
31 #include <sys/stat.h>
32 #include <string.h>
33 #include <zlib.h>
34 
35 #define	Z_BUFSIZE 2048	/* XXX larger? */
36 
37 struct z_file
38 {
39 	int		zf_rawfd;
40 	off_t		zf_dataoffset;
41 	z_stream	zf_zstream;
42 	unsigned char	zf_buf[Z_BUFSIZE];
43 	int		zf_endseen;
44 };
45 
46 static int zf_fill(struct z_file *z);
47 static int zf_open(const char *path, struct open_file *f);
48 static int zf_close(struct open_file *f);
49 static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
50 static off_t zf_seek(struct open_file *f, off_t offset, int where);
51 static int zf_stat(struct open_file *f, struct stat *sb);
52 
53 struct fs_ops gzipfs_fsops = {
54 	.fs_name = "zip",
55 	.fo_open = zf_open,
56 	.fo_close = zf_close,
57 	.fo_read = zf_read,
58 	.fo_write = null_write,
59 	.fo_seek = zf_seek,
60 	.fo_stat = zf_stat,
61 	.fo_readdir = null_readdir
62 };
63 
64 static int
65 zf_fill(struct z_file *zf)
66 {
67 	int	result;
68 	int	req;
69 
70 	req = Z_BUFSIZE - zf->zf_zstream.avail_in;
71 	result = 0;
72 
73 	/* If we need more */
74 	if (req > 0) {
75 		/* move old data to bottom of buffer */
76 		if (req < Z_BUFSIZE)
77 			bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
78 
79 		/* read to fill buffer and update availibility data */
80 		result = read(zf->zf_rawfd,
81 		    zf->zf_buf + zf->zf_zstream.avail_in, req);
82 		zf->zf_zstream.next_in = zf->zf_buf;
83 		if (result >= 0)
84 			zf->zf_zstream.avail_in += result;
85 	}
86 	return (result);
87 }
88 
89 /*
90  * Adapted from get_byte/check_header in libz
91  *
92  * Returns 0 if the header is OK, nonzero if not.
93  */
94 static int
95 get_byte(struct z_file *zf, off_t *curoffp)
96 {
97 	if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
98 		return (-1);
99 	zf->zf_zstream.avail_in--;
100 	++*curoffp;
101 	return (*(zf->zf_zstream.next_in)++);
102 }
103 
104 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
105 
106 /* gzip flag byte */
107 #define	ASCII_FLAG	0x01 /* bit 0 set: file probably ascii text */
108 #define	HEAD_CRC	0x02 /* bit 1 set: header CRC present */
109 #define	EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
110 #define	ORIG_NAME	0x08 /* bit 3 set: original file name present */
111 #define	COMMENT		0x10 /* bit 4 set: file comment present */
112 #define	RESERVED	0xE0 /* bits 5..7: reserved */
113 
114 static int
115 check_header(struct z_file *zf)
116 {
117 	int	method; /* method byte */
118 	int	flags;  /* flags byte */
119 	uInt	len;
120 	int	c;
121 
122 	zf->zf_dataoffset = 0;
123 	/* Check the gzip magic header */
124 	for (len = 0; len < 2; len++) {
125 		c = get_byte(zf, &zf->zf_dataoffset);
126 		if (c != gz_magic[len]) {
127 			return (1);
128 		}
129 	}
130 	method = get_byte(zf, &zf->zf_dataoffset);
131 	flags = get_byte(zf, &zf->zf_dataoffset);
132 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
133 		return (1);
134 	}
135 
136 	/* Discard time, xflags and OS code: */
137 	for (len = 0; len < 6; len++)
138 		(void) get_byte(zf, &zf->zf_dataoffset);
139 
140 	if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
141 		len  =  (uInt)get_byte(zf, &zf->zf_dataoffset);
142 		len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
143 		/* len is garbage if EOF but the loop below will quit anyway */
144 		while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1)
145 			;
146 	}
147 	if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
148 		while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1)
149 			;
150 	}
151 	if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
152 		while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1)
153 			;
154 	}
155 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
156 		for (len = 0; len < 2; len++)
157 			c = get_byte(zf, &zf->zf_dataoffset);
158 	}
159 	/* if there's data left, we're in business */
160 	return ((c == -1) ? 1 : 0);
161 }
162 
163 static int
164 zf_open(const char *fname, struct open_file *f)
165 {
166 	char		*zfname;
167 	int		rawfd;
168 	struct z_file	*zf;
169 	char		*cp;
170 	int		error;
171 	struct stat	sb;
172 
173 	/* Have to be in "just read it" mode */
174 	if (f->f_flags != F_READ)
175 		return (EPERM);
176 
177 	/* If the name already ends in .gz or .bz2, ignore it */
178 	if ((cp = strrchr(fname, '.')) && (strcmp(cp, ".gz") == 0 ||
179 	    strcmp(cp, ".bz2") == 0 || strcmp(cp, ".split") == 0))
180 		return (ENOENT);
181 
182 	/* Try to open the compressed datafile */
183 	rawfd = open(fname, O_RDONLY | F_GZIP);
184 	if (rawfd == -1) {
185 		/* add .gz sufix and try again */
186 		zfname = malloc(strlen(fname) + 4);
187 		if (zfname == NULL)
188 			return (ENOMEM);
189 		sprintf(zfname, "%s.gz", fname);
190 		rawfd = open(zfname, O_RDONLY);
191 		free(zfname);
192 		if (rawfd == -1)
193 			return (ENOENT);
194 	}
195 
196 	if (fstat(rawfd, &sb) < 0) {
197 		printf("zf_open: stat failed\n");
198 		close(rawfd);
199 		return (ENOENT);
200 	}
201 	if (!S_ISREG(sb.st_mode)) {
202 		close(rawfd);
203 		return (EISDIR);	/* best guess */
204 	}
205 
206 	/* Allocate a z_file structure, populate it */
207 	zf = malloc(sizeof (struct z_file));
208 	if (zf == NULL)
209 		return (ENOMEM);
210 	bzero(zf, sizeof (struct z_file));
211 	zf->zf_rawfd = rawfd;
212 
213 	/* Verify that the file is gzipped */
214 	if (check_header(zf)) {
215 		close(zf->zf_rawfd);
216 		free(zf);
217 		return (EFTYPE);
218 	}
219 
220 	/* Initialise the inflation engine */
221 	if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
222 		printf("zf_open: inflateInit returned %d : %s\n", error,
223 		    zf->zf_zstream.msg);
224 		close(zf->zf_rawfd);
225 		free(zf);
226 		return (EIO);
227 	}
228 
229 	/* Looks OK, we'll take it */
230 	f->f_fsdata = zf;
231 	return (0);
232 }
233 
234 static int
235 zf_close(struct open_file *f)
236 {
237 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
238 
239 	inflateEnd(&(zf->zf_zstream));
240 	close(zf->zf_rawfd);
241 	free(zf);
242 	return (0);
243 }
244 
245 static int
246 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
247 {
248 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
249 	int		error;
250 
251 	zf->zf_zstream.next_out = buf;		/* where and how much */
252 	zf->zf_zstream.avail_out = size;
253 
254 	while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
255 		if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
256 			printf("zf_read: fill error\n");
257 			return (EIO);
258 		}
259 		if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
260 			printf("zf_read: unexpected EOF\n");
261 			if (zf->zf_zstream.avail_out == size)
262 				return (EIO);
263 			break;
264 		}
265 
266 		/* decompression pass */
267 		error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH);
268 		if (error == Z_STREAM_END) {	/* EOF, all done */
269 			zf->zf_endseen = 1;
270 			break;
271 		}
272 		if (error != Z_OK) {		/* argh, decompression error */
273 			printf("inflate: %s\n", zf->zf_zstream.msg);
274 			return (EIO);
275 		}
276 	}
277 	if (resid != NULL)
278 		*resid = zf->zf_zstream.avail_out;
279 	return (0);
280 }
281 
282 static int
283 zf_rewind(struct open_file *f)
284 {
285 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
286 
287 	if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
288 		return (-1);
289 	zf->zf_zstream.avail_in = 0;
290 	zf->zf_zstream.next_in = NULL;
291 	zf->zf_endseen = 0;
292 	(void) inflateReset(&zf->zf_zstream);
293 
294 	return (0);
295 }
296 
297 static off_t
298 zf_seek(struct open_file *f, off_t offset, int where)
299 {
300 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
301 	off_t		target;
302 	char		discard[16];
303 
304 	switch (where) {
305 	case SEEK_SET:
306 		target = offset;
307 		break;
308 	case SEEK_CUR:
309 		target = offset + zf->zf_zstream.total_out;
310 		break;
311 	default:
312 		errno = EINVAL;
313 		return (-1);
314 	}
315 
316 	/* rewind if required */
317 	if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
318 		return (-1);
319 
320 	/* skip forwards if required */
321 	while (target > zf->zf_zstream.total_out) {
322 		errno = zf_read(f, discard, min(sizeof (discard),
323 		    target - zf->zf_zstream.total_out), NULL);
324 		if (errno != 0)
325 			return (-1);
326 	}
327 	/* This is where we are (be honest if we overshot) */
328 	return (zf->zf_zstream.total_out);
329 }
330 
331 
332 static int
333 zf_stat(struct open_file *f, struct stat *sb)
334 {
335 	struct z_file	*zf = (struct z_file *)f->f_fsdata;
336 	int		result;
337 	off_t		pos1, pos2;
338 	uint32_t	size;
339 
340 	/* stat as normal, but indicate that size is unknown */
341 	if ((result = fstat(zf->zf_rawfd, sb)) == 0) {
342 		if (sb->st_size == -1)
343 			return (result);
344 		pos1 = lseek(zf->zf_rawfd, 0, SEEK_CUR);
345 		pos2 = lseek(zf->zf_rawfd, sb->st_size - 4, SEEK_SET);
346 		if (pos2 != -1) {
347 			if (read(zf->zf_rawfd, &size, 4) == 4)
348 				sb->st_size = (off_t)size;
349 			else
350 				sb->st_size = -1;
351 		} else
352 			sb->st_size = -1;
353 
354 		pos1 = lseek(zf->zf_rawfd, pos1, SEEK_SET);
355 	}
356 	return (result);
357 }
358