]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libstand/gzipfs.c
Move ENTRY and ALTENTRY definitions to asm.h where they belong.
[FreeBSD/FreeBSD.git] / lib / libstand / gzipfs.c
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  * $FreeBSD$
27  *
28  */
29
30 #include "stand.h"
31
32 #include <sys/stat.h>
33 #include <string.h>
34 #include <zlib.h>
35
36 #define Z_BUFSIZE 2048  /* XXX larger? */
37
38 struct z_file
39 {
40     int                 zf_rawfd;
41     z_stream            zf_zstream;
42     char                zf_buf[Z_BUFSIZE];
43 };
44
45 static int      zf_fill(struct z_file *z);
46 static int      zf_open(const char *path, struct open_file *f);
47 static int      zf_close(struct open_file *f);
48 static int      zf_read(struct open_file *f, void *buf, size_t size, size_t *resid);
49 static off_t    zf_seek(struct open_file *f, off_t offset, int where);
50 static int      zf_stat(struct open_file *f, struct stat *sb);
51
52 struct fs_ops zipfs_fsops = {
53     "zip",
54     zf_open, 
55     zf_close, 
56     zf_read,
57     null_write,
58     zf_seek,
59     zf_stat
60 };
61
62 #if 0
63 void *
64 calloc(int items, size_t size)
65 {
66     return(malloc(items * size));
67 }
68 #endif
69
70 static int
71 zf_fill(struct z_file *zf)
72 {
73     int         result;
74     int         req;
75     
76     req = Z_BUFSIZE - zf->zf_zstream.avail_in;
77     result = 0;
78     
79     /* If we need more */
80     if (req > 0) {
81         /* move old data to bottom of buffer */
82         if (req < Z_BUFSIZE)
83             bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
84         
85         /* read to fill buffer and update availibility data */
86         result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
87         zf->zf_zstream.next_in = zf->zf_buf;
88         if (result >= 0)
89             zf->zf_zstream.avail_in += result;
90     }
91     return(result);
92 }
93
94 /*
95  * Adapted from get_byte/check_header in libz
96  *
97  * Returns 0 if the header is OK, nonzero if not.
98  */
99 static int
100 get_byte(struct z_file *zf)
101 {
102     if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
103         return(-1);
104     zf->zf_zstream.avail_in--;
105     return(*(zf->zf_zstream.next_in)++);
106 }
107
108 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
109
110 /* gzip flag byte */
111 #define ASCII_FLAG      0x01 /* bit 0 set: file probably ascii text */
112 #define HEAD_CRC        0x02 /* bit 1 set: header CRC present */
113 #define EXTRA_FIELD     0x04 /* bit 2 set: extra field present */
114 #define ORIG_NAME       0x08 /* bit 3 set: original file name present */
115 #define COMMENT         0x10 /* bit 4 set: file comment present */
116 #define RESERVED        0xE0 /* bits 5..7: reserved */
117
118 static int
119 check_header(struct z_file *zf)
120 {
121     int         method; /* method byte */
122     int         flags;  /* flags byte */
123     uInt        len;
124     int         c;
125
126     /* Check the gzip magic header */
127     for (len = 0; len < 2; len++) {
128         c = get_byte(zf);
129         if (c != gz_magic[len]) {
130             return(1);
131         }
132     }
133     method = get_byte(zf);
134     flags = get_byte(zf);
135     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
136         return(1);
137     }
138     
139     /* Discard time, xflags and OS code: */
140     for (len = 0; len < 6; len++) (void)get_byte(zf);
141
142     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
143         len  =  (uInt)get_byte(zf);
144         len += ((uInt)get_byte(zf))<<8;
145         /* len is garbage if EOF but the loop below will quit anyway */
146         while (len-- != 0 && get_byte(zf) != -1) ;
147     }
148     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
149         while ((c = get_byte(zf)) != 0 && c != -1) ;
150     }
151     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
152         while ((c = get_byte(zf)) != 0 && c != -1) ;
153     }
154     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
155         for (len = 0; len < 2; len++) c = get_byte(zf);
156     }
157     /* if there's data left, we're in business */
158     return((c == -1) ? 1 : 0);
159 }
160         
161 static int
162 zf_open(const char *fname, struct open_file *f)
163 {
164     static char         *zfname;
165     int                 rawfd;
166     struct z_file       *zf;
167     char                *cp;
168     int                 error;
169     struct stat         sb;
170
171     /* Have to be in "just read it" mode */
172     if (f->f_flags != F_READ)
173         return(EPERM);
174
175     /* If the name already ends in .gz, ignore it */
176     if ((cp = strrchr(fname, '.')) && !strcmp(cp, ".gz"))
177         return(ENOENT);
178
179     /* Construct new name */
180     zfname = malloc(strlen(fname) + 4);
181     sprintf(zfname, "%s.gz", fname);
182
183     /* Try to open the compressed datafile */
184     rawfd = open(zfname, O_RDONLY);
185     free(zfname);
186     if (rawfd == -1)
187         return(ENOENT);
188
189     if (fstat(rawfd, &sb) < 0) {
190         printf("zf_open: stat failed\n");
191         close(rawfd);
192         return(ENOENT);
193     }
194     if (!S_ISREG(sb.st_mode)) {
195         printf("zf_open: not a file\n");
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     bzero(zf, sizeof(struct z_file));
203     zf->zf_rawfd = rawfd;
204
205     /* Verify that the file is gzipped (XXX why do this afterwards?) */
206     if (check_header(zf)) {
207         close(zf->zf_rawfd);
208         inflateEnd(&(zf->zf_zstream));
209         free(zf);
210         return(EFTYPE);
211     }
212
213     /* Initialise the inflation engine */
214     if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
215         printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
216         close(zf->zf_rawfd);
217         free(zf);
218         return(EIO);
219     }
220
221     /* Looks OK, we'll take it */
222     f->f_fsdata = zf;
223     return(0);
224 }
225
226 static int
227 zf_close(struct open_file *f)
228 {
229     struct z_file       *zf = (struct z_file *)f->f_fsdata;
230     
231     inflateEnd(&(zf->zf_zstream));
232     close(zf->zf_rawfd);
233     free(zf);
234     return(0);
235 }
236  
237 static int 
238 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
239 {
240     struct z_file       *zf = (struct z_file *)f->f_fsdata;
241     int                 error;
242
243     zf->zf_zstream.next_out = buf;                      /* where and how much */
244     zf->zf_zstream.avail_out = size;
245
246     while (zf->zf_zstream.avail_out) {
247         if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
248             printf("zf_read: fill error\n");
249             return(-1);
250         }
251         if (zf->zf_zstream.avail_in == 0) {             /* oops, unexpected EOF */
252             printf("zf_read: unexpected EOF\n");
253             break;
254         }
255
256         error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
257         if (error == Z_STREAM_END) {                    /* EOF, all done */
258             break;
259         }
260         if (error != Z_OK) {                            /* argh, decompression error */
261             printf("inflate: %s\n", zf->zf_zstream.msg);
262             errno = EIO;
263             return(-1);
264         }
265     }
266     if (resid != NULL)
267         *resid = zf->zf_zstream.avail_out;
268     return(0);
269 }
270
271 static off_t
272 zf_seek(struct open_file *f, off_t offset, int where)
273 {
274     struct z_file       *zf = (struct z_file *)f->f_fsdata;
275     off_t               target;
276     char                discard[16];
277     
278     switch (where) {
279     case SEEK_SET:
280         target = offset;
281         break;
282     case SEEK_CUR:
283         target = offset + zf->zf_zstream.total_out;
284         break;
285     default:
286         target = -1;
287     }
288
289     /* Can we get there from here? */
290     if (target < zf->zf_zstream.total_out) {
291         errno = EOFFSET;
292         return -1;
293     } 
294
295     /* skip forwards if required */
296     while (target > zf->zf_zstream.total_out) {
297         if (zf_read(f, discard, min(sizeof(discard), target - zf->zf_zstream.total_out), NULL) == -1)
298             return(-1);
299     }
300     /* This is where we are (be honest if we overshot) */
301     return (zf->zf_zstream.total_out);
302 }
303
304
305 static int
306 zf_stat(struct open_file *f, struct stat *sb)
307 {
308     struct z_file       *zf = (struct z_file *)f->f_fsdata;
309     int                 result;
310
311     /* stat as normal, but indicate that size is unknown */
312     if ((result = fstat(zf->zf_rawfd, sb)) == 0)
313         sb->st_size = -1;
314     return(result);
315 }
316
317
318