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