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