loader: updates from review
[unleashed.git] / usr / src / boot / lib / libstand / gzipfs.c
blob1db7ad095b4e45f836f010534f3df7b885f8d145
1 /*
2 * Copyright (c) 1998 Michael Smith.
3 * All rights reserved.
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "stand.h"
32 #include <sys/stat.h>
33 #include <string.h>
34 #include <zlib.h>
36 #define Z_BUFSIZE 2048 /* XXX larger? */
38 struct z_file
40 int zf_rawfd;
41 off_t zf_dataoffset;
42 z_stream zf_zstream;
43 char zf_buf[Z_BUFSIZE];
44 int zf_endseen;
47 static int zf_fill(struct z_file *z);
48 static int zf_open(const char *path, struct open_file *f);
49 static int zf_close(struct open_file *f);
50 static int zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
51 static off_t zf_seek(struct open_file *f, off_t offset, int where);
52 static int zf_stat(struct open_file *f, struct stat *sb);
54 struct fs_ops gzipfs_fsops = {
55 "zip",
56 zf_open,
57 zf_close,
58 zf_read,
59 null_write,
60 zf_seek,
61 zf_stat,
62 null_readdir
65 static int
66 zf_fill(struct z_file *zf)
68 int result;
69 int req;
71 req = Z_BUFSIZE - zf->zf_zstream.avail_in;
72 result = 0;
74 /* If we need more */
75 if (req > 0) {
76 /* move old data to bottom of buffer */
77 if (req < Z_BUFSIZE)
78 bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
80 /* read to fill buffer and update availibility data */
81 result = read(zf->zf_rawfd, 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;
86 return(result);
90 * Adapted from get_byte/check_header in libz
92 * Returns 0 if the header is OK, nonzero if not.
94 static int
95 get_byte(struct z_file *zf, off_t *curoffp)
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)++);
104 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
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 */
114 static int
115 check_header(struct z_file *zf)
117 int method; /* method byte */
118 int flags; /* flags byte */
119 uInt len;
120 int c;
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);
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);
136 /* Discard time, xflags and OS code: */
137 for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
139 if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
140 len = (uInt)get_byte(zf, &zf->zf_dataoffset);
141 len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
142 /* len is garbage if EOF but the loop below will quit anyway */
143 while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
145 if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
146 while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
148 if ((flags & COMMENT) != 0) { /* skip the .gz file comment */
149 while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
151 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
152 for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
154 /* if there's data left, we're in business */
155 return((c == -1) ? 1 : 0);
158 static int
159 zf_open(const char *fname, struct open_file *f)
161 char *zfname;
162 int rawfd;
163 struct z_file *zf;
164 char *cp;
165 int error;
166 struct stat sb;
168 /* Have to be in "just read it" mode */
169 if (f->f_flags != F_READ)
170 return(EPERM);
172 /* If the name already ends in .gz or .bz2, ignore it */
173 if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
174 || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
175 return(ENOENT);
177 /* Try to open the compressed datafile */
178 rawfd = open(fname, O_RDONLY | F_GZIP);
179 if (rawfd == -1) {
180 /* add .gz sufix and try again */
181 zfname = malloc(strlen(fname) + 4);
182 if (zfname == NULL)
183 return(ENOMEM);
184 sprintf(zfname, "%s.gz", fname);
185 rawfd = open(zfname, O_RDONLY);
186 free(zfname);
187 if (rawfd == -1)
188 return(ENOENT);
191 if (fstat(rawfd, &sb) < 0) {
192 printf("zf_open: stat failed\n");
193 close(rawfd);
194 return(ENOENT);
196 if (!S_ISREG(sb.st_mode)) {
197 close(rawfd);
198 return(EISDIR); /* best guess */
201 /* Allocate a z_file structure, populate it */
202 zf = malloc(sizeof(struct z_file));
203 if (zf == NULL)
204 return(ENOMEM);
205 bzero(zf, sizeof(struct z_file));
206 zf->zf_rawfd = rawfd;
208 /* Verify that the file is gzipped */
209 if (check_header(zf)) {
210 close(zf->zf_rawfd);
211 free(zf);
212 return(EFTYPE);
215 /* Initialise the inflation engine */
216 if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
217 printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
218 close(zf->zf_rawfd);
219 free(zf);
220 return(EIO);
223 /* Looks OK, we'll take it */
224 f->f_fsdata = zf;
225 return(0);
228 static int
229 zf_close(struct open_file *f)
231 struct z_file *zf = (struct z_file *)f->f_fsdata;
233 inflateEnd(&(zf->zf_zstream));
234 close(zf->zf_rawfd);
235 free(zf);
236 return(0);
239 static int
240 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
242 struct z_file *zf = (struct z_file *)f->f_fsdata;
243 int error;
245 zf->zf_zstream.next_out = buf; /* where and how much */
246 zf->zf_zstream.avail_out = size;
248 while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
249 if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
250 printf("zf_read: fill error\n");
251 return(EIO);
253 if (zf->zf_zstream.avail_in == 0) { /* oops, unexpected EOF */
254 printf("zf_read: unexpected EOF\n");
255 if (zf->zf_zstream.avail_out == size)
256 return(EIO);
257 break;
260 error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
261 if (error == Z_STREAM_END) { /* EOF, all done */
262 zf->zf_endseen = 1;
263 break;
265 if (error != Z_OK) { /* argh, decompression error */
266 printf("inflate: %s\n", zf->zf_zstream.msg);
267 return(EIO);
270 if (resid != NULL)
271 *resid = zf->zf_zstream.avail_out;
272 return(0);
275 static int
276 zf_rewind(struct open_file *f)
278 struct z_file *zf = (struct z_file *)f->f_fsdata;
280 if (lseek(zf->zf_rawfd, zf->zf_dataoffset, SEEK_SET) == -1)
281 return(-1);
282 zf->zf_zstream.avail_in = 0;
283 zf->zf_zstream.next_in = NULL;
284 zf->zf_endseen = 0;
285 (void)inflateReset(&zf->zf_zstream);
287 return(0);
290 static off_t
291 zf_seek(struct open_file *f, off_t offset, int where)
293 struct z_file *zf = (struct z_file *)f->f_fsdata;
294 off_t target;
295 char discard[16];
297 switch (where) {
298 case SEEK_SET:
299 target = offset;
300 break;
301 case SEEK_CUR:
302 target = offset + zf->zf_zstream.total_out;
303 break;
304 case SEEK_END:
305 target = -1;
306 default:
307 errno = EINVAL;
308 return(-1);
311 /* rewind if required */
312 if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
313 return(-1);
315 /* skip forwards if required */
316 while (target > zf->zf_zstream.total_out) {
317 errno = zf_read(f, discard, min(sizeof(discard),
318 target - zf->zf_zstream.total_out), NULL);
319 if (errno)
320 return(-1);
322 /* This is where we are (be honest if we overshot) */
323 return(zf->zf_zstream.total_out);
327 static int
328 zf_stat(struct open_file *f, struct stat *sb)
330 struct z_file *zf = (struct z_file *)f->f_fsdata;
331 int result, res;
332 off_t pos1, pos2;
333 uint32_t size;
335 /* stat as normal, but indicate that size is unknown */
336 if ((result = fstat(zf->zf_rawfd, sb)) == 0) {
337 if (sb->st_size == -1)
338 return (result);
339 pos1 = lseek(zf->zf_rawfd, 0, SEEK_CUR);
340 pos2 = lseek(zf->zf_rawfd, sb->st_size - 4, SEEK_SET);
341 if (pos2 != -1) {
342 if (read(zf->zf_rawfd, &size, 4) == 4)
343 sb->st_size = (off_t) size;
344 else
345 sb->st_size = -1;
346 } else
347 sb->st_size = -1;
349 pos1 = lseek(zf->zf_rawfd, pos1, SEEK_SET);
351 return(result);