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