]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libstand/gzipfs.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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     off_t               zf_dataoffset;
42     z_stream            zf_zstream;
43     char                zf_buf[Z_BUFSIZE];
44     int                 zf_endseen;
45 };
46
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);
53
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
63 };
64
65 #if 0
66 void *
67 calloc(int items, size_t size)
68 {
69     return(malloc(items * size));
70 }
71 #endif
72
73 static int
74 zf_fill(struct z_file *zf)
75 {
76     int         result;
77     int         req;
78     
79     req = Z_BUFSIZE - zf->zf_zstream.avail_in;
80     result = 0;
81     
82     /* If we need more */
83     if (req > 0) {
84         /* move old data to bottom of buffer */
85         if (req < Z_BUFSIZE)
86             bcopy(zf->zf_buf + req, zf->zf_buf, Z_BUFSIZE - req);
87         
88         /* read to fill buffer and update availibility data */
89         result = read(zf->zf_rawfd, zf->zf_buf + zf->zf_zstream.avail_in, req);
90         zf->zf_zstream.next_in = zf->zf_buf;
91         if (result >= 0)
92             zf->zf_zstream.avail_in += result;
93     }
94     return(result);
95 }
96
97 /*
98  * Adapted from get_byte/check_header in libz
99  *
100  * Returns 0 if the header is OK, nonzero if not.
101  */
102 static int
103 get_byte(struct z_file *zf, off_t *curoffp)
104 {
105     if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1))
106         return(-1);
107     zf->zf_zstream.avail_in--;
108     ++*curoffp;
109     return(*(zf->zf_zstream.next_in)++);
110 }
111
112 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
113
114 /* gzip flag byte */
115 #define ASCII_FLAG      0x01 /* bit 0 set: file probably ascii text */
116 #define HEAD_CRC        0x02 /* bit 1 set: header CRC present */
117 #define EXTRA_FIELD     0x04 /* bit 2 set: extra field present */
118 #define ORIG_NAME       0x08 /* bit 3 set: original file name present */
119 #define COMMENT         0x10 /* bit 4 set: file comment present */
120 #define RESERVED        0xE0 /* bits 5..7: reserved */
121
122 static int
123 check_header(struct z_file *zf)
124 {
125     int         method; /* method byte */
126     int         flags;  /* flags byte */
127     uInt        len;
128     int         c;
129
130     zf->zf_dataoffset = 0;
131     /* Check the gzip magic header */
132     for (len = 0; len < 2; len++) {
133         c = get_byte(zf, &zf->zf_dataoffset);
134         if (c != gz_magic[len]) {
135             return(1);
136         }
137     }
138     method = get_byte(zf, &zf->zf_dataoffset);
139     flags = get_byte(zf, &zf->zf_dataoffset);
140     if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
141         return(1);
142     }
143     
144     /* Discard time, xflags and OS code: */
145     for (len = 0; len < 6; len++) (void)get_byte(zf, &zf->zf_dataoffset);
146
147     if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
148         len  =  (uInt)get_byte(zf, &zf->zf_dataoffset);
149         len += ((uInt)get_byte(zf, &zf->zf_dataoffset))<<8;
150         /* len is garbage if EOF but the loop below will quit anyway */
151         while (len-- != 0 && get_byte(zf, &zf->zf_dataoffset) != -1) ;
152     }
153     if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
154         while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
155     }
156     if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
157         while ((c = get_byte(zf, &zf->zf_dataoffset)) != 0 && c != -1) ;
158     }
159     if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
160         for (len = 0; len < 2; len++) c = get_byte(zf, &zf->zf_dataoffset);
161     }
162     /* if there's data left, we're in business */
163     return((c == -1) ? 1 : 0);
164 }
165         
166 static int
167 zf_open(const char *fname, struct open_file *f)
168 {
169     static char         *zfname;
170     int                 rawfd;
171     struct z_file       *zf;
172     char                *cp;
173     int                 error;
174     struct stat         sb;
175
176     /* Have to be in "just read it" mode */
177     if (f->f_flags != F_READ)
178         return(EPERM);
179
180     /* If the name already ends in .gz or .bz2, ignore it */
181     if ((cp = strrchr(fname, '.')) && (!strcmp(cp, ".gz")
182             || !strcmp(cp, ".bz2") || !strcmp(cp, ".split")))
183         return(ENOENT);
184
185     /* Construct new name */
186     zfname = malloc(strlen(fname) + 4);
187     if (zfname == NULL)
188         return(ENOMEM);
189     sprintf(zfname, "%s.gz", fname);
190
191     /* Try to open the compressed datafile */
192     rawfd = open(zfname, O_RDONLY);
193     free(zfname);
194     if (rawfd == -1)
195         return(ENOENT);
196
197     if (fstat(rawfd, &sb) < 0) {
198         printf("zf_open: stat failed\n");
199         close(rawfd);
200         return(ENOENT);
201     }
202     if (!S_ISREG(sb.st_mode)) {
203         printf("zf_open: not a file\n");
204         close(rawfd);
205         return(EISDIR);                 /* best guess */
206     }
207
208     /* Allocate a z_file structure, populate it */
209     zf = malloc(sizeof(struct z_file));
210     if (zf == NULL)
211         return(ENOMEM);
212     bzero(zf, sizeof(struct z_file));
213     zf->zf_rawfd = rawfd;
214
215     /* Verify that the file is gzipped */
216     if (check_header(zf)) {
217         close(zf->zf_rawfd);
218         free(zf);
219         return(EFTYPE);
220     }
221
222     /* Initialise the inflation engine */
223     if ((error = inflateInit2(&(zf->zf_zstream), -15)) != Z_OK) {
224         printf("zf_open: inflateInit returned %d : %s\n", error, zf->zf_zstream.msg);
225         close(zf->zf_rawfd);
226         free(zf);
227         return(EIO);
228     }
229
230     /* Looks OK, we'll take it */
231     f->f_fsdata = zf;
232     return(0);
233 }
234
235 static int
236 zf_close(struct open_file *f)
237 {
238     struct z_file       *zf = (struct z_file *)f->f_fsdata;
239     
240     inflateEnd(&(zf->zf_zstream));
241     close(zf->zf_rawfd);
242     free(zf);
243     return(0);
244 }
245  
246 static int 
247 zf_read(struct open_file *f, void *buf, size_t size, size_t *resid)
248 {
249     struct z_file       *zf = (struct z_file *)f->f_fsdata;
250     int                 error;
251
252     zf->zf_zstream.next_out = buf;                      /* where and how much */
253     zf->zf_zstream.avail_out = size;
254
255     while (zf->zf_zstream.avail_out && zf->zf_endseen == 0) {
256         if ((zf->zf_zstream.avail_in == 0) && (zf_fill(zf) == -1)) {
257             printf("zf_read: fill error\n");
258             return(EIO);
259         }
260         if (zf->zf_zstream.avail_in == 0) {             /* oops, unexpected EOF */
261             printf("zf_read: unexpected EOF\n");
262             if (zf->zf_zstream.avail_out == size)
263                 return(EIO);
264             break;
265         }
266
267         error = inflate(&zf->zf_zstream, Z_SYNC_FLUSH); /* decompression pass */
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     case SEEK_END:
312         target = -1;
313     default:
314         errno = EINVAL;
315         return(-1);
316     }
317
318     /* rewind if required */
319     if (target < zf->zf_zstream.total_out && zf_rewind(f) != 0)
320         return(-1);
321
322     /* skip forwards if required */
323     while (target > zf->zf_zstream.total_out) {
324         errno = zf_read(f, discard, min(sizeof(discard),
325             target - zf->zf_zstream.total_out), NULL);
326         if (errno)
327             return(-1);
328     }
329     /* This is where we are (be honest if we overshot) */
330     return(zf->zf_zstream.total_out);
331 }
332
333
334 static int
335 zf_stat(struct open_file *f, struct stat *sb)
336 {
337     struct z_file       *zf = (struct z_file *)f->f_fsdata;
338     int                 result;
339
340     /* stat as normal, but indicate that size is unknown */
341     if ((result = fstat(zf->zf_rawfd, sb)) == 0)
342         sb->st_size = -1;
343     return(result);
344 }
345
346
347