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