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