Home | History | Annotate | Line # | Download | only in libtos
cread.c revision 1.4.92.1
      1  1.4.92.1  jym /*	$NetBSD: cread.c,v 1.4.92.1 2009/05/13 17:16:33 jym 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.4.92.1  jym static int		get_byte(struct sd *);
     87  1.4.92.1  jym static unsigned long	getLong(struct sd *);
     88  1.4.92.1  jym static void		check_header(struct sd *);
     89       1.1  leo 
     90       1.1  leo /* XXX - find suitable headerf ile for these: */
     91  1.4.92.1  jym void	*zcalloc(void *, unsigned int, unsigned int);
     92  1.4.92.1  jym void	zcfree(void *, void *);
     93  1.4.92.1  jym void	zmemcpy(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.4.92.1  jym zcalloc (void *opaque, unsigned items, unsigned size)
    102       1.1  leo {
    103       1.1  leo 	return(malloc(items * size));
    104       1.1  leo }
    105       1.1  leo 
    106       1.1  leo void
    107  1.4.92.1  jym zcfree (void *opaque, void *ptr)
    108       1.1  leo {
    109       1.1  leo 	free(ptr);
    110       1.1  leo }
    111       1.1  leo 
    112       1.1  leo void
    113  1.4.92.1  jym zmemcpy(unsigned char *dest, unsigned char *source, unsigned int len)
    114       1.1  leo {
    115  1.4.92.1  jym 	memcpy(dest, source, len);
    116       1.1  leo }
    117       1.1  leo 
    118       1.1  leo static int
    119  1.4.92.1  jym get_byte(struct sd *s)
    120       1.1  leo {
    121       1.1  leo 	if (s->z_eof)
    122       1.1  leo 		return (EOF);
    123       1.1  leo 
    124       1.1  leo 	if (s->stream.avail_in == 0) {
    125       1.1  leo 		int got;
    126       1.1  leo 
    127       1.1  leo 		errno = 0;
    128       1.1  leo 		got = cread(s->fd, s->inbuf, Z_BUFSIZE);
    129       1.1  leo 		if (got <= 0) {
    130       1.1  leo 			s->z_eof = 1;
    131       1.1  leo 			if (errno) s->z_err = Z_ERRNO;
    132       1.1  leo 			return EOF;
    133       1.1  leo 		}
    134       1.1  leo 		s->stream.avail_in = got;
    135       1.1  leo 		s->stream.next_in = s->inbuf;
    136       1.1  leo 	}
    137       1.1  leo 	s->stream.avail_in--;
    138       1.1  leo 	return *(s->stream.next_in)++;
    139       1.1  leo }
    140       1.1  leo 
    141       1.1  leo static unsigned long
    142  1.4.92.1  jym getLong (struct sd *s)
    143       1.1  leo {
    144       1.1  leo 	unsigned long x = (unsigned long)get_byte(s);
    145       1.1  leo 	int c;
    146       1.1  leo 
    147       1.1  leo 	x += ((unsigned long)get_byte(s)) << 8;
    148       1.1  leo 	x += ((unsigned long)get_byte(s)) << 16;
    149       1.1  leo 	c = get_byte(s);
    150       1.1  leo 	if (c == EOF)
    151       1.1  leo 		s->z_err = Z_DATA_ERROR;
    152       1.1  leo 	x += ((unsigned long)c)<<24;
    153       1.1  leo 	return x;
    154       1.1  leo }
    155       1.1  leo 
    156       1.1  leo static void
    157  1.4.92.1  jym check_header(struct sd *s)
    158       1.1  leo {
    159       1.1  leo 	int method; /* method byte */
    160       1.1  leo 	int flags;  /* flags byte */
    161       1.1  leo 	unsigned int len;
    162       1.1  leo 	int c;
    163       1.1  leo 
    164       1.1  leo 	/* Check the gzip magic header */
    165       1.1  leo 	for (len = 0; len < 2; len++) {
    166       1.1  leo 		c = get_byte(s);
    167       1.1  leo 		if (c == gz_magic[len])
    168       1.1  leo 			continue;
    169       1.1  leo 		if ((c == EOF) && (len == 0))  {
    170       1.1  leo 			/*
    171       1.1  leo 			 * We must not change s->compressed if we are at EOF;
    172       1.1  leo 			 * we may have come to the end of a gzipped file and be
    173       1.1  leo 			 * check to see if another gzipped file is concatenated
    174       1.1  leo 			 * to this one. If one isn't, we still need to be able
    175       1.1  leo 			 * to lseek on this file as a compressed file.
    176       1.1  leo 			 */
    177       1.1  leo 			return;
    178       1.1  leo 		}
    179       1.1  leo 		s->compressed = 0;
    180       1.1  leo 		if (c != EOF) {
    181       1.1  leo 			s->stream.avail_in++;
    182       1.1  leo 			s->stream.next_in--;
    183       1.1  leo 		}
    184       1.1  leo 		s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
    185       1.1  leo 		return;
    186       1.1  leo 	}
    187       1.1  leo 	s->compressed = 1;
    188       1.1  leo 	method = get_byte(s);
    189       1.1  leo 	flags = get_byte(s);
    190       1.1  leo 	if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
    191       1.1  leo 		s->z_err = Z_DATA_ERROR;
    192       1.1  leo 		return;
    193       1.1  leo 	}
    194       1.1  leo 
    195       1.1  leo 	/* Discard time, xflags and OS code: */
    196       1.1  leo 	for (len = 0; len < 6; len++)
    197       1.1  leo 		(void)get_byte(s);
    198       1.1  leo 
    199       1.1  leo 	if ((flags & EXTRA_FIELD) != 0) {
    200       1.1  leo 		/* skip the extra field */
    201       1.1  leo 		len  =  (unsigned int)get_byte(s);
    202       1.1  leo 		len += ((unsigned int)get_byte(s)) << 8;
    203       1.1  leo 		/* len is garbage if EOF but the loop below will quit anyway */
    204       1.1  leo 		while (len-- != 0 && get_byte(s) != EOF) /*void*/;
    205       1.1  leo 	}
    206       1.1  leo 	if ((flags & ORIG_NAME) != 0) {
    207       1.1  leo 		/* skip the original file name */
    208       1.1  leo 		while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
    209       1.1  leo 	}
    210       1.1  leo 	if ((flags & COMMENT) != 0) {
    211       1.1  leo 		/* skip the .gz file comment */
    212       1.1  leo 		while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
    213       1.1  leo 	}
    214       1.1  leo 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
    215       1.1  leo 		for (len = 0; len < 2; len++)
    216       1.1  leo 			(void)get_byte(s);
    217       1.1  leo 	}
    218       1.1  leo 	s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
    219       1.1  leo }
    220       1.1  leo 
    221       1.1  leo /*
    222       1.1  leo  * new open(), close(), read(), lseek()
    223       1.1  leo  */
    224       1.1  leo 
    225       1.1  leo int
    226  1.4.92.1  jym copen(const char *fname, int mode)
    227       1.1  leo {
    228       1.1  leo 	int fd;
    229       1.1  leo 	struct sd *s = 0;
    230       1.1  leo 
    231       1.1  leo 	if ( ((fd = open(fname, mode)) == -1) || (mode != O_RDONLY) )
    232       1.1  leo 		/* compression only for read */
    233       1.1  leo 		return(fd);
    234       1.1  leo 
    235       1.1  leo 	ss[fd] = s = malloc(sizeof(struct sd));
    236       1.1  leo 	if (s == 0)
    237       1.1  leo 		goto errout;
    238  1.4.92.1  jym 	memset(s, 0, sizeof(struct sd));
    239       1.1  leo 
    240       1.1  leo 	if (inflateInit2(&(s->stream), -15) != Z_OK)
    241       1.1  leo 		goto errout;
    242       1.1  leo 
    243       1.1  leo 	s->stream.next_in  = s->inbuf = (unsigned char*)malloc(Z_BUFSIZE);
    244       1.1  leo 	if (s->inbuf == 0) {
    245       1.1  leo 		inflateEnd(&(s->stream));
    246       1.1  leo 		goto errout;
    247       1.1  leo 	}
    248       1.1  leo 
    249       1.1  leo 	s->fd = fd;
    250       1.1  leo 	check_header(s); /* skip the .gz header */
    251       1.1  leo 	return(fd);
    252       1.1  leo 
    253       1.1  leo errout:
    254       1.1  leo 	if (s != 0)
    255       1.1  leo 		free(s);
    256       1.1  leo 	close(fd);
    257       1.1  leo 	return (-1);
    258       1.1  leo }
    259       1.1  leo 
    260       1.1  leo int
    261  1.4.92.1  jym cclose(int fd)
    262       1.1  leo {
    263       1.1  leo 	struct sd *s;
    264       1.1  leo 
    265       1.1  leo 	s = ss[fd];
    266       1.1  leo 
    267       1.1  leo 	inflateEnd(&(s->stream));
    268       1.1  leo 
    269       1.1  leo 	free(s->inbuf);
    270       1.1  leo 	free(s);
    271       1.1  leo 
    272       1.1  leo 	return (close(fd));
    273       1.1  leo }
    274       1.1  leo 
    275       1.1  leo size_t
    276  1.4.92.1  jym cread(int fd, void *buf, size_t len)
    277       1.1  leo {
    278       1.1  leo 	struct sd *s;
    279       1.1  leo 	unsigned char *start = buf; /* starting point for crc computation */
    280       1.1  leo 
    281       1.1  leo 	s = ss[fd];
    282       1.1  leo 
    283       1.1  leo 	if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
    284       1.1  leo 		return (-1);
    285       1.1  leo 	if (s->z_err == Z_STREAM_END)
    286       1.1  leo 		return (0);  /* EOF */
    287       1.1  leo 
    288       1.1  leo 	s->stream.next_out = buf;
    289       1.1  leo 	s->stream.avail_out = len;
    290       1.1  leo 
    291       1.1  leo 	while (s->stream.avail_out != 0) {
    292       1.1  leo 
    293       1.1  leo 		if (s->compressed == 0) {
    294       1.1  leo 			/* Copy first the lookahead bytes: */
    295       1.1  leo 			unsigned int n = s->stream.avail_in;
    296       1.1  leo 			if (n > s->stream.avail_out)
    297       1.1  leo 				n = s->stream.avail_out;
    298       1.1  leo 			if (n > 0) {
    299       1.1  leo 				zmemcpy(s->stream.next_out,
    300       1.1  leo 					s->stream.next_in, n);
    301       1.1  leo 				s->stream.next_out  += n;
    302       1.1  leo 				s->stream.next_in   += n;
    303       1.1  leo 				s->stream.avail_out -= n;
    304       1.1  leo 				s->stream.avail_in  -= n;
    305       1.1  leo 			}
    306       1.1  leo 			if (s->stream.avail_out > 0) {
    307       1.1  leo 				int got;
    308       1.1  leo 				got = read(s->fd, s->stream.next_out,
    309       1.1  leo 					    s->stream.avail_out);
    310       1.1  leo 				if (got == -1)
    311       1.1  leo 					return (got);
    312       1.1  leo 				s->stream.avail_out -= got;
    313       1.1  leo 			}
    314       1.1  leo 			return (int)(len - s->stream.avail_out);
    315       1.1  leo 		}
    316       1.1  leo 
    317       1.1  leo 		if (s->stream.avail_in == 0 && !s->z_eof) {
    318       1.1  leo 			int got;
    319       1.1  leo 			errno = 0;
    320       1.1  leo 			got = read(fd, s->inbuf, Z_BUFSIZE);
    321       1.1  leo 			if (got <= 0) {
    322       1.1  leo 				s->z_eof = 1;
    323       1.1  leo 				if (errno) {
    324       1.1  leo 					s->z_err = Z_ERRNO;
    325       1.1  leo 					break;
    326       1.1  leo 				}
    327       1.1  leo 			}
    328       1.1  leo 			s->stream.avail_in = got;
    329       1.1  leo 			s->stream.next_in = s->inbuf;
    330       1.1  leo 		}
    331       1.1  leo 
    332       1.1  leo 		s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
    333       1.1  leo 
    334       1.1  leo 		if (s->z_err == Z_STREAM_END) {
    335       1.1  leo 			/* Check CRC and original size */
    336       1.1  leo 			s->crc = crc32(s->crc, start, (unsigned int)
    337       1.1  leo 					(s->stream.next_out - start));
    338       1.1  leo 			start = s->stream.next_out;
    339       1.1  leo 
    340       1.1  leo 			if (getLong(s) != s->crc ||
    341       1.1  leo 			    getLong(s) != s->stream.total_out) {
    342       1.1  leo 
    343       1.1  leo 				s->z_err = Z_DATA_ERROR;
    344       1.1  leo 			} else {
    345       1.1  leo 				/* Check for concatenated .gz files: */
    346       1.1  leo 				check_header(s);
    347       1.1  leo 				if (s->z_err == Z_OK) {
    348       1.1  leo 					inflateReset(&(s->stream));
    349       1.1  leo 					s->crc = crc32(0L, Z_NULL, 0);
    350       1.1  leo 				}
    351       1.1  leo 			}
    352       1.1  leo 		}
    353       1.1  leo 		if (s->z_err != Z_OK || s->z_eof)
    354       1.1  leo 			break;
    355       1.1  leo 	}
    356       1.1  leo 
    357       1.1  leo 	s->crc = crc32(s->crc, start,
    358       1.1  leo 		       (unsigned int)(s->stream.next_out - start));
    359       1.1  leo 
    360       1.1  leo 	return (int)(len - s->stream.avail_out);
    361       1.1  leo }
    362       1.1  leo 
    363       1.1  leo off_t
    364  1.4.92.1  jym clseek(int fd, off_t offset, int where)
    365       1.1  leo {
    366       1.1  leo 	struct sd *s;
    367       1.1  leo 
    368       1.1  leo 	s = ss[fd];
    369       1.1  leo 
    370       1.1  leo 	if(s->compressed == 0) {
    371       1.1  leo 		off_t res = lseek(fd, offset, where);
    372       1.1  leo 		if (res != (off_t)-1) {
    373       1.1  leo 			/* make sure the lookahead buffer is invalid */
    374       1.1  leo 			s->stream.avail_in = 0;
    375       1.1  leo 		}
    376       1.1  leo 		return (res);
    377       1.1  leo 	}
    378       1.1  leo 
    379       1.1  leo 	switch(where) {
    380       1.1  leo 	case SEEK_CUR:
    381       1.1  leo 		    offset += s->stream.total_out;
    382       1.1  leo 	case SEEK_SET:
    383       1.1  leo 		/* if seek backwards, simply start from the beginning */
    384       1.1  leo 		if (offset < s->stream.total_out) {
    385       1.1  leo 			off_t res;
    386       1.1  leo 			void *sav_inbuf;
    387       1.1  leo 
    388       1.1  leo 			res = lseek(fd, 0, SEEK_SET);
    389       1.1  leo 			if(res == (off_t)-1)
    390       1.1  leo 			    return(res);
    391       1.1  leo 			/* ??? perhaps fallback to close / open */
    392       1.1  leo 
    393       1.1  leo 			inflateEnd(&(s->stream));
    394       1.1  leo 
    395       1.1  leo 			sav_inbuf = s->inbuf; /* don't allocate again */
    396  1.4.92.1  jym 			memset(s, 0, sizeof(struct sd)); /* this resets total_out to 0! */
    397       1.1  leo 
    398       1.1  leo 			inflateInit2(&(s->stream), -15);
    399       1.1  leo 			s->stream.next_in = s->inbuf = sav_inbuf;
    400       1.1  leo 
    401       1.1  leo 			s->fd = fd;
    402       1.1  leo 			check_header(s); /* skip the .gz header */
    403       1.1  leo 		}
    404       1.1  leo 
    405       1.1  leo 		    /* to seek forwards, throw away data */
    406       1.1  leo 		if (offset > s->stream.total_out) {
    407       1.1  leo 			off_t toskip = offset - s->stream.total_out;
    408       1.1  leo 
    409       1.1  leo 			while (toskip > 0) {
    410       1.1  leo #define DUMMYBUFSIZE 256
    411       1.1  leo 				char dummybuf[DUMMYBUFSIZE];
    412       1.1  leo 				off_t len = toskip;
    413       1.1  leo 				if (len > DUMMYBUFSIZE) len = DUMMYBUFSIZE;
    414       1.1  leo 				if (cread(fd, dummybuf, len) != len) {
    415       1.1  leo 					errno = EINVAL;
    416       1.1  leo 					return ((off_t)-1);
    417       1.1  leo 				}
    418       1.1  leo 				toskip -= len;
    419       1.1  leo 			}
    420       1.1  leo 		}
    421       1.1  leo #ifdef DEBUG
    422       1.1  leo 		if (offset != s->stream.total_out)
    423       1.1  leo 			panic("lseek compressed");
    424       1.1  leo #endif
    425       1.1  leo 		return (offset);
    426       1.1  leo 	case SEEK_END:
    427       1.1  leo 		errno = EINVAL;
    428       1.1  leo 		break;
    429       1.1  leo 	default:
    430       1.1  leo 		errno = EINVAL;
    431       1.1  leo 	}
    432       1.1  leo 
    433       1.1  leo 	return((off_t)-1);
    434       1.1  leo }
    435