1 /* $NetBSD: cread.c,v 1.9 2009/03/18 17:06:43 cegger Exp $ */
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 * Support for compressed bootfiles (only read)
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.
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
44 #define _CREAD_C /* Turn of open/close/read redefines */
54 #define __P(proto) proto
58 #define EOF (-1) /* needed by compression code */
61 #define Z_BUFSIZE 1024
63 #define Z_BUFSIZE 32*1024
66 static int gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
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 */
78 int z_err
; /* error code for last stream operation */
79 int z_eof
; /* set if end of input file */
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 */
86 static int get_byte(struct sd
*);
87 static unsigned long getLong(struct sd
*);
88 static void check_header(struct sd
*);
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);
97 * compression utilities
101 zcalloc (void *opaque
, unsigned items
, unsigned size
)
103 return(malloc(items
* size
));
107 zcfree (void *opaque
, void *ptr
)
113 zmemcpy(unsigned char *dest
, unsigned char *source
, unsigned int len
)
115 memcpy(dest
, source
, len
);
119 get_byte(struct sd
*s
)
124 if (s
->stream
.avail_in
== 0) {
128 got
= cread(s
->fd
, s
->inbuf
, Z_BUFSIZE
);
131 if (errno
) s
->z_err
= Z_ERRNO
;
134 s
->stream
.avail_in
= got
;
135 s
->stream
.next_in
= s
->inbuf
;
137 s
->stream
.avail_in
--;
138 return *(s
->stream
.next_in
)++;
142 getLong (struct sd
*s
)
144 unsigned long x
= (unsigned long)get_byte(s
);
147 x
+= ((unsigned long)get_byte(s
)) << 8;
148 x
+= ((unsigned long)get_byte(s
)) << 16;
151 s
->z_err
= Z_DATA_ERROR
;
152 x
+= ((unsigned long)c
)<<24;
157 check_header(struct sd
*s
)
159 int method
; /* method byte */
160 int flags
; /* flags byte */
164 /* Check the gzip magic header */
165 for (len
= 0; len
< 2; len
++) {
167 if (c
== gz_magic
[len
])
169 if ((c
== EOF
) && (len
== 0)) {
171 * We must not change s->compressed if we are at EOF;
172 * we may have come to the end of a gzipped file and be
173 * check to see if another gzipped file is concatenated
174 * to this one. If one isn't, we still need to be able
175 * to lseek on this file as a compressed file.
181 s
->stream
.avail_in
++;
184 s
->z_err
= s
->stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
188 method
= get_byte(s
);
190 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
191 s
->z_err
= Z_DATA_ERROR
;
195 /* Discard time, xflags and OS code: */
196 for (len
= 0; len
< 6; len
++)
199 if ((flags
& EXTRA_FIELD
) != 0) {
200 /* skip the extra field */
201 len
= (unsigned int)get_byte(s
);
202 len
+= ((unsigned int)get_byte(s
)) << 8;
203 /* len is garbage if EOF but the loop below will quit anyway */
204 while (len
-- != 0 && get_byte(s
) != EOF
) /*void*/;
206 if ((flags
& ORIG_NAME
) != 0) {
207 /* skip the original file name */
208 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) /*void*/;
210 if ((flags
& COMMENT
) != 0) {
211 /* skip the .gz file comment */
212 while ((c
= get_byte(s
)) != 0 && c
!= EOF
) /*void*/;
214 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
215 for (len
= 0; len
< 2; len
++)
218 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
222 * new open(), close(), read(), lseek()
226 copen(const char *fname
, int mode
)
231 if ( ((fd
= open(fname
, mode
)) == -1) || (mode
!= O_RDONLY
) )
232 /* compression only for read */
235 ss
[fd
] = s
= malloc(sizeof(struct sd
));
238 memset(s
, 0, sizeof(struct sd
));
240 if (inflateInit2(&(s
->stream
), -15) != Z_OK
)
243 s
->stream
.next_in
= s
->inbuf
= (unsigned char*)malloc(Z_BUFSIZE
);
245 inflateEnd(&(s
->stream
));
250 check_header(s
); /* skip the .gz header */
267 inflateEnd(&(s
->stream
));
276 cread(int fd
, void *buf
, size_t len
)
279 unsigned char *start
= buf
; /* starting point for crc computation */
283 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
)
285 if (s
->z_err
== Z_STREAM_END
)
286 return (0); /* EOF */
288 s
->stream
.next_out
= buf
;
289 s
->stream
.avail_out
= len
;
291 while (s
->stream
.avail_out
!= 0) {
293 if (s
->compressed
== 0) {
294 /* Copy first the lookahead bytes: */
295 unsigned int n
= s
->stream
.avail_in
;
296 if (n
> s
->stream
.avail_out
)
297 n
= s
->stream
.avail_out
;
299 zmemcpy(s
->stream
.next_out
,
300 s
->stream
.next_in
, n
);
301 s
->stream
.next_out
+= n
;
302 s
->stream
.next_in
+= n
;
303 s
->stream
.avail_out
-= n
;
304 s
->stream
.avail_in
-= n
;
306 if (s
->stream
.avail_out
> 0) {
308 got
= read(s
->fd
, s
->stream
.next_out
,
309 s
->stream
.avail_out
);
312 s
->stream
.avail_out
-= got
;
314 return (int)(len
- s
->stream
.avail_out
);
317 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
320 got
= read(fd
, s
->inbuf
, Z_BUFSIZE
);
328 s
->stream
.avail_in
= got
;
329 s
->stream
.next_in
= s
->inbuf
;
332 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
334 if (s
->z_err
== Z_STREAM_END
) {
335 /* Check CRC and original size */
336 s
->crc
= crc32(s
->crc
, start
, (unsigned int)
337 (s
->stream
.next_out
- start
));
338 start
= s
->stream
.next_out
;
340 if (getLong(s
) != s
->crc
||
341 getLong(s
) != s
->stream
.total_out
) {
343 s
->z_err
= Z_DATA_ERROR
;
345 /* Check for concatenated .gz files: */
347 if (s
->z_err
== Z_OK
) {
348 inflateReset(&(s
->stream
));
349 s
->crc
= crc32(0L, Z_NULL
, 0);
353 if (s
->z_err
!= Z_OK
|| s
->z_eof
)
357 s
->crc
= crc32(s
->crc
, start
,
358 (unsigned int)(s
->stream
.next_out
- start
));
360 return (int)(len
- s
->stream
.avail_out
);
364 clseek(int fd
, off_t offset
, int where
)
370 if(s
->compressed
== 0) {
371 off_t res
= lseek(fd
, offset
, where
);
372 if (res
!= (off_t
)-1) {
373 /* make sure the lookahead buffer is invalid */
374 s
->stream
.avail_in
= 0;
381 offset
+= s
->stream
.total_out
;
383 /* if seek backwards, simply start from the beginning */
384 if (offset
< s
->stream
.total_out
) {
388 res
= lseek(fd
, 0, SEEK_SET
);
391 /* ??? perhaps fallback to close / open */
393 inflateEnd(&(s
->stream
));
395 sav_inbuf
= s
->inbuf
; /* don't allocate again */
396 memset(s
, 0, sizeof(struct sd
)); /* this resets total_out to 0! */
398 inflateInit2(&(s
->stream
), -15);
399 s
->stream
.next_in
= s
->inbuf
= sav_inbuf
;
402 check_header(s
); /* skip the .gz header */
405 /* to seek forwards, throw away data */
406 if (offset
> s
->stream
.total_out
) {
407 off_t toskip
= offset
- s
->stream
.total_out
;
410 #define DUMMYBUFSIZE 256
411 char dummybuf
[DUMMYBUFSIZE
];
413 if (len
> DUMMYBUFSIZE
) len
= DUMMYBUFSIZE
;
414 if (cread(fd
, dummybuf
, len
) != len
) {
422 if (offset
!= s
->stream
.total_out
)
423 panic("lseek compressed");