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