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