vm: fix potential null deref
[minix.git] / sys / lib / libsa / cread.c
blob2eac7b2b13c08928c87b00bf836d1be7c2314384
1 /* $NetBSD: cread.c,v 1.23 2009/03/25 18:41:06 tls Exp $ */
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Support for compressed bootfiles (only read)
32 * - replaces open(), close(), read(), lseek().
33 * - original libsa open(), close(), read(), lseek() are called
34 * as oopen(), oclose(), oread() resp. olseek().
35 * - compression parts stripped from zlib:gzio.c
38 /* gzio.c -- IO on .gz files
39 * Copyright (C) 1995-1996 Jean-loup Gailly.
40 * For conditions of distribution and use, see copyright notice in zlib.h
43 #include "stand.h"
44 #ifdef _STANDALONE
45 #include <lib/libkern/libkern.h>
46 #include <lib/libz/libz.h>
47 #else
48 #include <string.h>
49 #include <zlib.h>
50 #endif
52 #define EOF (-1) /* needed by compression code */
54 #ifdef SAVE_MEMORY
55 #define Z_BUFSIZE 1024
56 #else
57 #define Z_BUFSIZE 4096
58 #endif
60 static const int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
62 /* gzip flag byte */
63 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
64 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
65 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
66 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
67 #define COMMENT 0x10 /* bit 4 set: file comment present */
68 #define RESERVED 0xE0 /* bits 5..7: reserved */
70 static struct sd {
71 z_stream stream;
72 int z_err; /* error code for last stream operation */
73 int z_eof; /* set if end of input file */
74 int fd;
75 unsigned char *inbuf; /* input buffer */
76 unsigned long crc; /* crc32 of uncompressed data */
77 int compressed; /* 1 if input file is a .gz file */
78 } *ss[SOPEN_MAX];
80 static int get_byte(struct sd *);
81 static unsigned long getLong(struct sd *);
82 static void check_header(struct sd *);
84 /* XXX - find suitable header file for these: */
85 void *zcalloc(void *, unsigned int, unsigned int);
86 void zcfree(void *, void *);
87 void zmemcpy(unsigned char *, unsigned char *, unsigned int);
90 * The libkern version of this function uses an 8K set of tables.
91 * This is the double-loop version of LE CRC32 from if_ethersubr,
92 * lightly modified -- it is 200 bytes smaller than the version using
93 * a 4-bit table and at least 8K smaller than the libkern version.
95 #ifndef ETHER_CRC_POLY_LE
96 #define ETHER_CRC_POLY_LE 0xedb88320
97 #endif
98 #ifndef __minix
99 uint32_t
100 crc32(uint32_t crc, const uint8_t *const buf, size_t len)
101 #else
102 uLong crc32(uLong crc, const Bytef *buf, uInt len)
103 #endif
105 uint32_t c, carry;
106 size_t i, j;
108 crc = 0xffffffffU ^ crc;
109 for (i = 0; i < len; i++) {
110 c = buf[i];
111 for (j = 0; j < 8; j++) {
112 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
113 crc >>= 1;
114 c >>= 1;
115 if (carry) {
116 crc = (crc ^ ETHER_CRC_POLY_LE);
120 return (crc ^ 0xffffffffU);
124 * compression utilities
127 void *
128 zcalloc(void *opaque, unsigned int items, unsigned int size)
131 return alloc(items * size);
134 void
135 zcfree(void *opaque, void *ptr)
138 dealloc(ptr, 0); /* XXX works only with modified allocator */
141 void
142 zmemcpy(unsigned char *dest, unsigned char *source, unsigned int len)
145 memcpy(dest, source, len);
148 static int
149 get_byte(struct sd *s)
151 if (s->z_eof)
152 return EOF;
154 if (s->stream.avail_in == 0) {
155 int got;
157 errno = 0;
158 got = oread(s->fd, s->inbuf, Z_BUFSIZE);
159 if (got <= 0) {
160 s->z_eof = 1;
161 if (errno)
162 s->z_err = Z_ERRNO;
163 return EOF;
165 s->stream.avail_in = got;
166 s->stream.next_in = s->inbuf;
168 s->stream.avail_in--;
169 return *(s->stream.next_in)++;
172 static unsigned long
173 getLong(struct sd *s)
175 unsigned long x;
176 int c;
178 x = (unsigned long)get_byte(s);
179 x += ((unsigned long)get_byte(s)) << 8;
180 x += ((unsigned long)get_byte(s)) << 16;
181 c = get_byte(s);
182 if (c == EOF)
183 s->z_err = Z_DATA_ERROR;
184 x += ((unsigned long)c) << 24;
185 return x;
188 static void
189 check_header(struct sd *s)
191 int method; /* method byte */
192 int flags; /* flags byte */
193 unsigned int len;
194 int c;
196 /* Check the gzip magic header */
197 for (len = 0; len < 2; len++) {
198 c = get_byte(s);
199 if (c == gz_magic[len])
200 continue;
201 if ((c == EOF) && (len == 0)) {
203 * We must not change s->compressed if we are at EOF;
204 * we may have come to the end of a gzipped file and be
205 * check to see if another gzipped file is concatenated
206 * to this one. If one isn't, we still need to be able
207 * to lseek on this file as a compressed file.
209 return;
211 s->compressed = 0;
212 if (c != EOF) {
213 s->stream.avail_in++;
214 s->stream.next_in--;
216 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
217 return;
219 s->compressed = 1;
220 method = get_byte(s);
221 flags = get_byte(s);
222 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
223 s->z_err = Z_DATA_ERROR;
224 return;
227 /* Discard time, xflags and OS code: */
228 for (len = 0; len < 6; len++)
229 (void)get_byte(s);
231 if ((flags & EXTRA_FIELD) != 0) {
232 /* skip the extra field */
233 len = (unsigned int)get_byte(s);
234 len += ((unsigned int)get_byte(s)) << 8;
235 /* len is garbage if EOF but the loop below will quit anyway */
236 while (len-- != 0 && get_byte(s) != EOF)
237 /*void*/;
239 if ((flags & ORIG_NAME) != 0) {
240 /* skip the original file name */
241 while ((c = get_byte(s)) != 0 && c != EOF)
242 /*void*/;
244 if ((flags & COMMENT) != 0) {
245 /* skip the .gz file comment */
246 while ((c = get_byte(s)) != 0 && c != EOF)
247 /*void*/;
249 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
250 for (len = 0; len < 2; len++)
251 (void)get_byte(s);
253 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
257 * new open(), close(), read(), lseek()
261 open(const char *fname, int mode)
263 int fd;
264 struct sd *s = 0;
266 if (((fd = oopen(fname, mode)) == -1) || (mode != 0))
267 /* compression only for read */
268 return fd;
270 ss[fd] = s = alloc(sizeof(struct sd));
271 if (s == 0)
272 goto errout;
273 (void)memset(s, 0, sizeof(struct sd));
275 if (inflateInit2(&(s->stream), -15) != Z_OK)
276 goto errout;
278 s->stream.next_in = s->inbuf = (unsigned char *)alloc(Z_BUFSIZE);
279 if (s->inbuf == 0) {
280 inflateEnd(&(s->stream));
281 goto errout;
284 s->fd = fd;
285 check_header(s); /* skip the .gz header */
286 return fd;
288 errout:
289 if (s != 0)
290 dealloc(s, sizeof(struct sd));
291 oclose(fd);
292 return -1;
296 close(int fd)
298 struct open_file *f;
299 struct sd *s;
301 #if !defined(LIBSA_NO_FD_CHECKING)
302 if ((unsigned int)fd >= SOPEN_MAX) {
303 errno = EBADF;
304 return -1;
306 #endif
307 f = &files[fd];
309 if ((f->f_flags & F_READ) == 0)
310 return oclose(fd);
312 s = ss[fd];
314 inflateEnd(&(s->stream));
316 dealloc(s->inbuf, Z_BUFSIZE);
317 dealloc(s, sizeof(struct sd));
319 return oclose(fd);
322 ssize_t
323 read(int fd, void *buf, size_t len)
325 struct sd *s;
326 unsigned char *start = buf; /* starting point for crc computation */
328 s = ss[fd];
330 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
331 return -1;
332 if (s->z_err == Z_STREAM_END)
333 return 0; /* EOF */
335 s->stream.next_out = buf;
336 s->stream.avail_out = len;
338 while (s->stream.avail_out != 0) {
340 if (s->compressed == 0) {
341 /* Copy first the lookahead bytes: */
342 unsigned int n = s->stream.avail_in;
343 if (n > s->stream.avail_out)
344 n = s->stream.avail_out;
345 if (n > 0) {
346 zmemcpy(s->stream.next_out,
347 s->stream.next_in, n);
348 s->stream.next_out += n;
349 s->stream.next_in += n;
350 s->stream.avail_out -= n;
351 s->stream.avail_in -= n;
353 if (s->stream.avail_out > 0) {
354 int got;
355 got = oread(s->fd, s->stream.next_out,
356 s->stream.avail_out);
357 if (got == -1)
358 return got;
359 s->stream.avail_out -= got;
361 return (int)(len - s->stream.avail_out);
364 if (s->stream.avail_in == 0 && !s->z_eof) {
365 int got;
366 errno = 0;
367 got = oread(fd, s->inbuf, Z_BUFSIZE);
368 if (got <= 0) {
369 s->z_eof = 1;
370 if (errno) {
371 s->z_err = Z_ERRNO;
372 break;
375 s->stream.avail_in = got;
376 s->stream.next_in = s->inbuf;
379 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
381 if (s->z_err == Z_STREAM_END) {
382 /* Check CRC and original size */
383 s->crc = crc32(s->crc, start, (unsigned int)
384 (s->stream.next_out - start));
385 start = s->stream.next_out;
387 if (getLong(s) != s->crc ||
388 getLong(s) != s->stream.total_out) {
390 s->z_err = Z_DATA_ERROR;
391 } else {
392 /* Check for concatenated .gz files: */
393 check_header(s);
394 if (s->z_err == Z_OK) {
395 inflateReset(&(s->stream));
396 s->crc = crc32(0L, Z_NULL, 0);
400 if (s->z_err != Z_OK || s->z_eof)
401 break;
404 s->crc = crc32(s->crc, start,
405 (unsigned int)(s->stream.next_out - start));
407 return (int)(len - s->stream.avail_out);
410 off_t
411 lseek(int fd, off_t offset, int where)
413 struct open_file *f;
414 struct sd *s;
416 #if !defined(LIBSA_NO_FD_CHECKING)
417 if ((unsigned int)fd >= SOPEN_MAX) {
418 errno = EBADF;
419 return -1;
421 #endif
422 f = &files[fd];
424 if ((f->f_flags & F_READ) == 0)
425 return olseek(fd, offset, where);
427 s = ss[fd];
429 if(s->compressed == 0) {
430 off_t res = olseek(fd, offset, where);
431 if (res != (off_t)-1) {
432 /* make sure the lookahead buffer is invalid */
433 s->stream.avail_in = 0;
435 return res;
438 switch(where) {
439 case SEEK_CUR:
440 offset += s->stream.total_out;
441 case SEEK_SET:
442 /* if seek backwards, simply start from the beginning */
443 if (offset < s->stream.total_out) {
444 off_t res;
445 void *sav_inbuf;
447 res = olseek(fd, 0, SEEK_SET);
448 if(res == (off_t)-1)
449 return res;
450 /* ??? perhaps fallback to close / open */
452 inflateEnd(&(s->stream));
454 sav_inbuf = s->inbuf; /* don't allocate again */
455 (void)memset(s, 0, sizeof(struct sd));
456 /* this resets total_out to 0! */
458 inflateInit2(&(s->stream), -15);
459 s->stream.next_in = s->inbuf = sav_inbuf;
461 s->fd = fd;
462 check_header(s); /* skip the .gz header */
465 /* to seek forwards, throw away data */
466 if (offset > s->stream.total_out) {
467 off_t toskip = offset - s->stream.total_out;
469 while (toskip > 0) {
470 #define DUMMYBUFSIZE 256
471 char dummybuf[DUMMYBUFSIZE];
472 off_t len = toskip;
474 if (len > DUMMYBUFSIZE)
475 len = DUMMYBUFSIZE;
476 if (read(fd, dummybuf, len) != len) {
477 errno = EOFFSET;
478 return (off_t)-1;
480 toskip -= len;
483 #ifdef DEBUG
484 if (offset != s->stream.total_out)
485 panic("lseek compressed");
486 #endif
487 return offset;
488 case SEEK_END:
489 errno = EOFFSET;
490 break;
491 default:
492 errno = EINVAL;
493 break;
496 return (off_t)-1;