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