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