xref: /titanic_52/usr/src/boot/lib/libstand/gzipfs.c (revision 5c420b564d29aecc3d2a4e4b8718a21a952cab9b)
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     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     "zip",
55     zf_open,
56     zf_close,
57     zf_read,
58     null_write,
59     zf_seek,
60     zf_stat,
61     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, zf->zf_buf + zf->zf_zstream.avail_in, req);
81 	zf->zf_zstream.next_in = zf->zf_buf;
82 	if (result >= 0)
83 	    zf->zf_zstream.avail_in += result;
84     }
85     return(result);
86 }
87 
88 /*
89  * Adapted from get_byte/check_header in libz
90  *
91  * Returns 0 if the header is OK, nonzero if not.
92  */
93 static int
94 get_byte(struct z_file *zf, off_t *curoffp)
95 {
96     if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
97 	return(-1);
98     zf->zf_zstream.avail_in--;
99     ++*curoffp;
100     return(*(zf->zf_zstream.next_in)++);
101 }
102 
103 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
104 
105 /* gzip flag byte */
106 #define ASCII_FLAG	0x01 /* bit 0 set: file probably ascii text */
107 #define HEAD_CRC	0x02 /* bit 1 set: header CRC present */
108 #define EXTRA_FIELD	0x04 /* bit 2 set: extra field present */
109 #define ORIG_NAME	0x08 /* bit 3 set: original file name present */
110 #define COMMENT		0x10 /* bit 4 set: file comment present */
111 #define RESERVED	0xE0 /* bits 5..7: reserved */
112 
113 static int
114 check_header(struct z_file *zf)
115 {
116     int		method; /* method byte */
117     int		flags;  /* flags byte */
118     uInt	len;
119     int		c;
120 
121     zf->zf_dataoffset = 0;
122     /* Check the gzip magic header */
123     for (len = 0; len < 2; len++) {
124 	c = get_byte(zf, &zf->zf_dataoffset);
125 	if (c != gz_magic[len]) {
126 	    return(1);
127 	}
128     }
129     method = get_byte(zf, &zf->zf_dataoffset);
130     flags = get_byte(zf, &zf->zf_dataoffset);
131     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
132 	return(1);
133     }
134 
135     /* Discard time, xflags and OS code: */
136     for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
137 
138     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
139 	len  =  (uInt)get_byte(zf, &zf->zf_dataoffset);
140 	len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
141 	/* len is garbage if EOF but the loop below will quit anyway */
142 	while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
143     }
144     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
145 	while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
146     }
147     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
148 	while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
149     }
150     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
151 	for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
152     }
153     /* if there's data left, we're in business */
154     return((c == -1) ? 1 : 0);
155 }
156 
157 static int
158 zf_open(const char *fname, struct open_file *f)
159 {
160     char		*zfname;
161     int			rawfd;
162     struct z_file	*zf;
163     char		*cp;
164     int			error;
165     struct stat		sb;
166 
167     /* Have to be in "just read it" mode */
168     if (f->f_flags != F_READ)
169 	return(EPERM);
170 
171     /* If the name already ends in .gz or .bz2, ignore it */
172     if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
173 	    || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
174 	return(ENOENT);
175 
176     /* Try to open the compressed datafile */
177     rawfd = open(fname, O_RDONLY | F_GZIP);
178     if (rawfd == -1) {
179 	/* add .gz sufix and try again */
180 	zfname = malloc(strlen(fname) + 4);
181 	if (zfname == NULL)
182 		return(ENOMEM);
183 	sprintf(zfname, "%s.gz", fname);
184 	rawfd = open(zfname, O_RDONLY);
185 	free(zfname);
186 	if (rawfd == -1)
187 		return(ENOENT);
188     }
189 
190     if (fstat(rawfd, &sb) < 0) {
191 	printf("zf_open: stat failed\n");
192 	close(rawfd);
193 	return(ENOENT);
194     }
195     if (!S_ISREG(sb.st_mode)) {
196 	close(rawfd);
197 	return(EISDIR);			/* best guess */
198     }
199 
200     /* Allocate a z_file structure, populate it */
201     zf = malloc(sizeof(struct z_file));
202     if (zf == NULL)
203         return(ENOMEM);
204     bzero(zf, sizeof(struct z_file));
205     zf->zf_rawfd = rawfd;
206 
207     /* Verify that the file is gzipped */
208     if (check_header(zf)) {
209 	close(zf->zf_rawfd);
210 	free(zf);
211 	return(EFTYPE);
212     }
213 
214     /* Initialise the inflation engine */
215     if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
216 	printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
217 	close(zf->zf_rawfd);
218 	free(zf);
219 	return(EIO);
220     }
221 
222     /* Looks OK, we'll take it */
223     f->f_fsdata = zf;
224     return(0);
225 }
226 
227 static int
228 zf_close(struct open_file *f)
229 {
230     struct z_file	*zf = (struct z_file *)f->f_fsdata;
231 
232     inflateEnd(&(zf->zf_zstream));
233     close(zf->zf_rawfd);
234     free(zf);
235     return(0);
236 }
237 
238 static int
239 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
240 {
241     struct z_file	*zf = (struct z_file *)f->f_fsdata;
242     int			error;
243 
244     zf->zf_zstream.next_out = buf;			/* where and how much */
245     zf->zf_zstream.avail_out = size;
246 
247     while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
248 	if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
249 	    printf("zf_read: fill error\n");
250 	    return(EIO);
251 	}
252 	if (zf->zf_zstream.avail_in == 0) {		/* oops, unexpected EOF */
253 	    printf("zf_read: unexpected EOF\n");
254 	    if (zf->zf_zstream.avail_out == size)
255 		return(EIO);
256 	    break;
257 	}
258 
259 	error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH);	/* decompression pass */
260 	if (error == Z_STREAM_END) {			/* EOF, all done */
261 	    zf->zf_endseen = 1;
262 	    break;
263 	}
264 	if (error != Z_OK) {				/* argh, decompression error */
265 	    printf("inflate: %s\n", zf->zf_zstream.msg);
266 	    return(EIO);
267 	}
268     }
269     if (resid != NULL)
270 	*resid = zf->zf_zstream.avail_out;
271     return(0);
272 }
273 
274 static int
275 zf_rewind(struct open_file *f)
276 {
277     struct z_file	*zf = (struct z_file *)f->f_fsdata;
278 
279     if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
280 	return(-1);
281     zf->zf_zstream.avail_in = 0;
282     zf->zf_zstream.next_in = NULL;
283     zf->zf_endseen = 0;
284     (void)inflateReset(&zf->zf_zstream);
285 
286     return(0);
287 }
288 
289 static off_t
290 zf_seek(struct open_file *f, off_t offset, int where)
291 {
292     struct z_file	*zf = (struct z_file *)f->f_fsdata;
293     off_t		target;
294     char		discard[16];
295 
296     switch (where) {
297     case SEEK_SET:
298 	target = offset;
299 	break;
300     case SEEK_CUR:
301 	target = offset + zf->zf_zstream.total_out;
302 	break;
303     default:
304 	errno = EINVAL;
305 	return(-1);
306     }
307 
308     /* rewind if required */
309     if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
310 	return(-1);
311 
312     /* skip forwards if required */
313     while (target > zf->zf_zstream.total_out) {
314 	errno = zf_read(f, discard, min(sizeof(discard),
315 	    target - zf->zf_zstream.total_out), NULL);
316 	if (errno)
317 	    return(-1);
318     }
319     /* This is where we are (be honest if we overshot) */
320     return(zf->zf_zstream.total_out);
321 }
322 
323 
324 static int
325 zf_stat(struct open_file *f, struct stat *sb)
326 {
327     struct z_file	*zf = (struct z_file *)f->f_fsdata;
328     int			result, res;
329     off_t		pos1, pos2;
330     uint32_t		size;
331 
332     /* stat as normal, but indicate that size is unknown */
333     if ((result = fstat(zf->zf_rawfd, sb)) == 0) {
334 	if (sb->st_size == -1)
335 	    return (result);
336 	pos1 = lseek(zf->zf_rawfd, 0, SEEK_CUR);
337 	pos2 = lseek(zf->zf_rawfd, sb->st_size - 4, SEEK_SET);
338 	if (pos2 != -1) {
339 	    if (read(zf->zf_rawfd, &size, 4) == 4)
340 		sb->st_size = (off_t) size;
341 	    else
342 		sb->st_size = -1;
343 	} else
344 	    sb->st_size = -1;
345 
346 	pos1 = lseek(zf->zf_rawfd, pos1, SEEK_SET);
347     }
348     return(result);
349 }
350