Home | History | Annotate | Line # | Download | only in libtos
cread.c revision 1.5
      1 /*	$NetBSD: cread.c,v 1.5 2009/03/14 14:45:57 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      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.
     15  *
     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.
     26  *
     27  */
     28 
     29 /*
     30  * Support for compressed bootfiles  (only read)
     31  *
     32  * - provides copen(), cclose(), cread(), clseek().
     33  * - compression parts stripped from zlib:gzio.c
     34  * - copied from libsa with small modifications for my MiNT environment.
     35  *   Note that everything in the 'tostools' hierarchy is made to function
     36  *   in my local MiNT environment.
     37  */
     38 
     39 /* gzio.c -- IO on .gz files
     40  * Copyright (C) 1995-1996 Jean-loup Gailly.
     41  * For conditions of distribution and use, see copyright notice in zlib.h
     42  */
     43 
     44 #define _CREAD_C	/* Turn of open/close/read redefines */
     45 
     46 #include <unistd.h>
     47 #include <string.h>
     48 #include <memory.h>
     49 #include <fcntl.h>
     50 #include <errno.h>
     51 #include <zlib.h>
     52 #include <cread.h>
     53 
     54 #define __P(proto)		proto
     55 #define	SOPEN_MAX		1
     56 
     57 
     58 #define EOF (-1) /* needed by compression code */
     59 
     60 #ifdef SAVE_MEMORY
     61 #define Z_BUFSIZE 1024
     62 #else
     63 #define Z_BUFSIZE 32*1024
     64 #endif
     65 
     66 static int gz_magic[2] = {0x1f, 0x8b};	/* gzip magic header */
     67 
     68 /* gzip flag byte */
     69 #define ASCII_FLAG	0x01	/* bit 0 set: file probably ascii text */
     70 #define HEAD_CRC	0x02	/* bit 1 set: header CRC present */
     71 #define EXTRA_FIELD	0x04	/* bit 2 set: extra field present */
     72 #define ORIG_NAME	0x08	/* bit 3 set: original file name present */
     73 #define COMMENT		0x10	/* bit 4 set: file comment present */
     74 #define RESERVED	0xE0	/* bits 5..7: reserved */
     75 
     76 static struct sd {
     77 	z_stream	stream;
     78 	int		z_err;	/* error code for last stream operation */
     79 	int		z_eof;	/* set if end of input file */
     80 	int		fd;
     81 	unsigned char	*inbuf;	/* input buffer */
     82 	unsigned long	crc;	/* crc32 of uncompressed data */
     83 	int		compressed;	/* 1 if input file is a .gz file */
     84 } *ss[SOPEN_MAX];
     85 
     86 static int		get_byte(struct sd *);
     87 static unsigned long	getLong(struct sd *);
     88 static void		check_header(struct sd *);
     89 
     90 /* XXX - find suitable headerf ile for these: */
     91 void	*zcalloc(void *, unsigned int, unsigned int);
     92 void	zcfree(void *, void *);
     93 void	zmemcpy(unsigned char *, unsigned char *, unsigned int);
     94 
     95 
     96 /*
     97  * compression utilities
     98  */
     99 
    100 void *
    101 zcalloc (opaque, items, size)
    102 	void *opaque;
    103 	unsigned items;
    104 	unsigned size;
    105 {
    106 	return(malloc(items * size));
    107 }
    108 
    109 void
    110 zcfree (opaque, ptr)
    111 	void *opaque;
    112 	void *ptr;
    113 {
    114 	free(ptr);
    115 }
    116 
    117 void
    118 zmemcpy(dest, source, len)
    119 	unsigned char *dest;
    120 	unsigned char *source;
    121 	unsigned int len;
    122 {
    123 	bcopy(source, dest, len);
    124 }
    125 
    126 static int
    127 get_byte(s)
    128 	struct sd *s;
    129 {
    130 	if (s->z_eof)
    131 		return (EOF);
    132 
    133 	if (s->stream.avail_in == 0) {
    134 		int got;
    135 
    136 		errno = 0;
    137 		got = cread(s->fd, s->inbuf, Z_BUFSIZE);
    138 		if (got <= 0) {
    139 			s->z_eof = 1;
    140 			if (errno) s->z_err = Z_ERRNO;
    141 			return EOF;
    142 		}
    143 		s->stream.avail_in = got;
    144 		s->stream.next_in = s->inbuf;
    145 	}
    146 	s->stream.avail_in--;
    147 	return *(s->stream.next_in)++;
    148 }
    149 
    150 static unsigned long
    151 getLong (s)
    152     struct sd *s;
    153 {
    154 	unsigned long x = (unsigned long)get_byte(s);
    155 	int c;
    156 
    157 	x += ((unsigned long)get_byte(s)) << 8;
    158 	x += ((unsigned long)get_byte(s)) << 16;
    159 	c = get_byte(s);
    160 	if (c == EOF)
    161 		s->z_err = Z_DATA_ERROR;
    162 	x += ((unsigned long)c)<<24;
    163 	return x;
    164 }
    165 
    166 static void
    167 check_header(s)
    168 	struct sd *s;
    169 {
    170 	int method; /* method byte */
    171 	int flags;  /* flags byte */
    172 	unsigned int len;
    173 	int c;
    174 
    175 	/* Check the gzip magic header */
    176 	for (len = 0; len < 2; len++) {
    177 		c = get_byte(s);
    178 		if (c == gz_magic[len])
    179 			continue;
    180 		if ((c == EOF) && (len == 0))  {
    181 			/*
    182 			 * We must not change s->compressed if we are at EOF;
    183 			 * we may have come to the end of a gzipped file and be
    184 			 * check to see if another gzipped file is concatenated
    185 			 * to this one. If one isn't, we still need to be able
    186 			 * to lseek on this file as a compressed file.
    187 			 */
    188 			return;
    189 		}
    190 		s->compressed = 0;
    191 		if (c != EOF) {
    192 			s->stream.avail_in++;
    193 			s->stream.next_in--;
    194 		}
    195 		s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
    196 		return;
    197 	}
    198 	s->compressed = 1;
    199 	method = get_byte(s);
    200 	flags = get_byte(s);
    201 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
    202 		s->z_err = Z_DATA_ERROR;
    203 		return;
    204 	}
    205 
    206 	/* Discard time, xflags and OS code: */
    207 	for (len = 0; len < 6; len++)
    208 		(void)get_byte(s);
    209 
    210 	if ((flags & EXTRA_FIELD) != 0) {
    211 		/* skip the extra field */
    212 		len  =  (unsigned int)get_byte(s);
    213 		len += ((unsigned int)get_byte(s)) << 8;
    214 		/* len is garbage if EOF but the loop below will quit anyway */
    215 		while (len-- != 0 && get_byte(s) != EOF) /*void*/;
    216 	}
    217 	if ((flags & ORIG_NAME) != 0) {
    218 		/* skip the original file name */
    219 		while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
    220 	}
    221 	if ((flags & COMMENT) != 0) {
    222 		/* skip the .gz file comment */
    223 		while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
    224 	}
    225 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
    226 		for (len = 0; len < 2; len++)
    227 			(void)get_byte(s);
    228 	}
    229 	s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
    230 }
    231 
    232 /*
    233  * new open(), close(), read(), lseek()
    234  */
    235 
    236 int
    237 copen(fname, mode)
    238 	const char *fname;
    239 	int mode;
    240 {
    241 	int fd;
    242 	struct sd *s = 0;
    243 
    244 	if ( ((fd = open(fname, mode)) == -1) || (mode != O_RDONLY) )
    245 		/* compression only for read */
    246 		return(fd);
    247 
    248 	ss[fd] = s = malloc(sizeof(struct sd));
    249 	if (s == 0)
    250 		goto errout;
    251 	bzero(s, sizeof(struct sd));
    252 
    253 	if (inflateInit2(&(s->stream), -15) != Z_OK)
    254 		goto errout;
    255 
    256 	s->stream.next_in  = s->inbuf = (unsigned char*)malloc(Z_BUFSIZE);
    257 	if (s->inbuf == 0) {
    258 		inflateEnd(&(s->stream));
    259 		goto errout;
    260 	}
    261 
    262 	s->fd = fd;
    263 	check_header(s); /* skip the .gz header */
    264 	return(fd);
    265 
    266 errout:
    267 	if (s != 0)
    268 		free(s);
    269 	close(fd);
    270 	return (-1);
    271 }
    272 
    273 int
    274 cclose(fd)
    275 	int fd;
    276 {
    277 	struct sd *s;
    278 
    279 	s = ss[fd];
    280 
    281 	inflateEnd(&(s->stream));
    282 
    283 	free(s->inbuf);
    284 	free(s);
    285 
    286 	return (close(fd));
    287 }
    288 
    289 size_t
    290 cread(fd, buf, len)
    291 	int fd;
    292 	void *buf;
    293 	size_t len;
    294 {
    295 	struct sd *s;
    296 	unsigned char *start = buf; /* starting point for crc computation */
    297 
    298 	s = ss[fd];
    299 
    300 	if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
    301 		return (-1);
    302 	if (s->z_err == Z_STREAM_END)
    303 		return (0);  /* EOF */
    304 
    305 	s->stream.next_out = buf;
    306 	s->stream.avail_out = len;
    307 
    308 	while (s->stream.avail_out != 0) {
    309 
    310 		if (s->compressed == 0) {
    311 			/* Copy first the lookahead bytes: */
    312 			unsigned int n = s->stream.avail_in;
    313 			if (n > s->stream.avail_out)
    314 				n = s->stream.avail_out;
    315 			if (n > 0) {
    316 				zmemcpy(s->stream.next_out,
    317 					s->stream.next_in, n);
    318 				s->stream.next_out  += n;
    319 				s->stream.next_in   += n;
    320 				s->stream.avail_out -= n;
    321 				s->stream.avail_in  -= n;
    322 			}
    323 			if (s->stream.avail_out > 0) {
    324 				int got;
    325 				got = read(s->fd, s->stream.next_out,
    326 					    s->stream.avail_out);
    327 				if (got == -1)
    328 					return (got);
    329 				s->stream.avail_out -= got;
    330 			}
    331 			return (int)(len - s->stream.avail_out);
    332 		}
    333 
    334 		if (s->stream.avail_in == 0 && !s->z_eof) {
    335 			int got;
    336 			errno = 0;
    337 			got = read(fd, s->inbuf, Z_BUFSIZE);
    338 			if (got <= 0) {
    339 				s->z_eof = 1;
    340 				if (errno) {
    341 					s->z_err = Z_ERRNO;
    342 					break;
    343 				}
    344 			}
    345 			s->stream.avail_in = got;
    346 			s->stream.next_in = s->inbuf;
    347 		}
    348 
    349 		s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
    350 
    351 		if (s->z_err == Z_STREAM_END) {
    352 			/* Check CRC and original size */
    353 			s->crc = crc32(s->crc, start, (unsigned int)
    354 					(s->stream.next_out - start));
    355 			start = s->stream.next_out;
    356 
    357 			if (getLong(s) != s->crc ||
    358 			    getLong(s) != s->stream.total_out) {
    359 
    360 				s->z_err = Z_DATA_ERROR;
    361 			} else {
    362 				/* Check for concatenated .gz files: */
    363 				check_header(s);
    364 				if (s->z_err == Z_OK) {
    365 					inflateReset(&(s->stream));
    366 					s->crc = crc32(0L, Z_NULL, 0);
    367 				}
    368 			}
    369 		}
    370 		if (s->z_err != Z_OK || s->z_eof)
    371 			break;
    372 	}
    373 
    374 	s->crc = crc32(s->crc, start,
    375 		       (unsigned int)(s->stream.next_out - start));
    376 
    377 	return (int)(len - s->stream.avail_out);
    378 }
    379 
    380 off_t
    381 clseek(fd, offset, where)
    382 	int fd;
    383 	off_t offset;
    384 	int where;
    385 {
    386 	struct sd *s;
    387 
    388 	s = ss[fd];
    389 
    390 	if(s->compressed == 0) {
    391 		off_t res = lseek(fd, offset, where);
    392 		if (res != (off_t)-1) {
    393 			/* make sure the lookahead buffer is invalid */
    394 			s->stream.avail_in = 0;
    395 		}
    396 		return (res);
    397 	}
    398 
    399 	switch(where) {
    400 	case SEEK_CUR:
    401 		    offset += s->stream.total_out;
    402 	case SEEK_SET:
    403 		/* if seek backwards, simply start from the beginning */
    404 		if (offset < s->stream.total_out) {
    405 			off_t res;
    406 			void *sav_inbuf;
    407 
    408 			res = lseek(fd, 0, SEEK_SET);
    409 			if(res == (off_t)-1)
    410 			    return(res);
    411 			/* ??? perhaps fallback to close / open */
    412 
    413 			inflateEnd(&(s->stream));
    414 
    415 			sav_inbuf = s->inbuf; /* don't allocate again */
    416 			bzero(s, sizeof(struct sd)); /* this resets total_out to 0! */
    417 
    418 			inflateInit2(&(s->stream), -15);
    419 			s->stream.next_in = s->inbuf = sav_inbuf;
    420 
    421 			s->fd = fd;
    422 			check_header(s); /* skip the .gz header */
    423 		}
    424 
    425 		    /* to seek forwards, throw away data */
    426 		if (offset > s->stream.total_out) {
    427 			off_t toskip = offset - s->stream.total_out;
    428 
    429 			while (toskip > 0) {
    430 #define DUMMYBUFSIZE 256
    431 				char dummybuf[DUMMYBUFSIZE];
    432 				off_t len = toskip;
    433 				if (len > DUMMYBUFSIZE) len = DUMMYBUFSIZE;
    434 				if (cread(fd, dummybuf, len) != len) {
    435 					errno = EINVAL;
    436 					return ((off_t)-1);
    437 				}
    438 				toskip -= len;
    439 			}
    440 		}
    441 #ifdef DEBUG
    442 		if (offset != s->stream.total_out)
    443 			panic("lseek compressed");
    444 #endif
    445 		return (offset);
    446 	case SEEK_END:
    447 		errno = EINVAL;
    448 		break;
    449 	default:
    450 		errno = EINVAL;
    451 	}
    452 
    453 	return((off_t)-1);
    454 }
    455