1 /* $NetBSD: cread.c,v 1.27 2015/07/25 07:06:11 isaki 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 * - replaces open(), close(), read(), lseek().
33 * - original libsa open(), close(), read(), lseek() are called
34 * as oopen(), oclose(), oread() resp. olseek().
35 * - compression parts stripped from zlib:gzio.c
38 /* gzio.c -- IO on .gz files
39 * Copyright (C) 1995-1996 Jean-loup Gailly.
40 * For conditions of distribution and use, see copyright notice in zlib.h
45 #include <lib/libkern/libkern.h>
46 #include <lib/libz/libz.h>
52 #define EOF (-1) /* needed by compression code */
55 #define Z_BUFSIZE 1024
57 #define Z_BUFSIZE 4096
60 static const int gz_magic
[2] = {0x1f, 0x8b}; /* gzip magic header */
63 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
64 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
65 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
66 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
67 #define COMMENT 0x10 /* bit 4 set: file comment present */
68 #define RESERVED 0xE0 /* bits 5..7: reserved */
72 int z_err
; /* error code for last stream operation */
73 int z_eof
; /* set if end of input file */
75 unsigned char *inbuf
; /* input buffer */
76 unsigned long crc
; /* crc32 of uncompressed data */
77 int compressed
; /* 1 if input file is a .gz file */
80 static int get_byte(struct sd
*);
81 static unsigned long getLong(struct sd
*);
82 static void check_header(struct sd
*);
84 /* XXX - find suitable header file for these: */
85 void *zcalloc(void *, unsigned int, unsigned int);
86 void zcfree(void *, void *);
87 void zmemcpy(unsigned char *, unsigned char *, unsigned int);
90 * The libkern version of this function uses an 8K set of tables.
91 * This is the double-loop version of LE CRC32 from if_ethersubr,
92 * lightly modified -- it is 200 bytes smaller than the version using
93 * a 4-bit table and at least 8K smaller than the libkern version.
95 #ifndef ETHER_CRC_POLY_LE
96 #define ETHER_CRC_POLY_LE 0xedb88320
99 crc32(uint32_t crc
, const uint8_t *const buf
, size_t len
)
104 crc
= 0xffffffffU
^ crc
;
105 for (i
= 0; i
< len
; i
++) {
107 for (j
= 0; j
< 8; j
++) {
108 carry
= ((crc
& 0x01) ? 1 : 0) ^ (c
& 0x01);
112 crc
= (crc
^ ETHER_CRC_POLY_LE
);
116 return (crc
^ 0xffffffffU
);
120 * compression utilities
124 zcalloc(void *opaque
, unsigned int items
, unsigned int size
)
127 return alloc(items
* size
);
131 zcfree(void *opaque
, void *ptr
)
134 dealloc(ptr
, 0); /* XXX works only with modified allocator */
138 zmemcpy(unsigned char *dest
, unsigned char *source
, unsigned int len
)
141 memcpy(dest
, source
, len
);
145 get_byte(struct sd
*s
)
150 if (s
->stream
.avail_in
== 0) {
154 got
= oread(s
->fd
, s
->inbuf
, Z_BUFSIZE
);
161 s
->stream
.avail_in
= got
;
162 s
->stream
.next_in
= s
->inbuf
;
164 s
->stream
.avail_in
--;
165 return *(s
->stream
.next_in
)++;
169 getLong(struct sd
*s
)
174 x
= (unsigned long)get_byte(s
);
175 x
+= ((unsigned long)get_byte(s
)) << 8;
176 x
+= ((unsigned long)get_byte(s
)) << 16;
179 s
->z_err
= Z_DATA_ERROR
;
180 x
+= ((unsigned long)c
) << 24;
185 check_header(struct sd
*s
)
187 int method
; /* method byte */
188 int flags
; /* flags byte */
192 /* Check the gzip magic header */
193 for (len
= 0; len
< 2; len
++) {
195 if (c
== gz_magic
[len
])
197 if ((c
== EOF
) && (len
== 0)) {
199 * We must not change s->compressed if we are at EOF;
200 * we may have come to the end of a gzipped file and be
201 * check to see if another gzipped file is concatenated
202 * to this one. If one isn't, we still need to be able
203 * to lseek on this file as a compressed file.
209 s
->stream
.avail_in
++;
212 s
->z_err
= s
->stream
.avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
216 method
= get_byte(s
);
218 if (method
!= Z_DEFLATED
|| (flags
& RESERVED
) != 0) {
219 s
->z_err
= Z_DATA_ERROR
;
223 /* Discard time, xflags and OS code: */
224 for (len
= 0; len
< 6; len
++)
227 if ((flags
& EXTRA_FIELD
) != 0) {
228 /* skip the extra field */
229 len
= (unsigned int)get_byte(s
);
230 len
+= ((unsigned int)get_byte(s
)) << 8;
231 /* len is garbage if EOF but the loop below will quit anyway */
232 while (len
-- != 0 && get_byte(s
) != EOF
)
235 if ((flags
& ORIG_NAME
) != 0) {
236 /* skip the original file name */
237 while ((c
= get_byte(s
)) != 0 && c
!= EOF
)
240 if ((flags
& COMMENT
) != 0) {
241 /* skip the .gz file comment */
242 while ((c
= get_byte(s
)) != 0 && c
!= EOF
)
245 if ((flags
& HEAD_CRC
) != 0) { /* skip the header crc */
246 for (len
= 0; len
< 2; len
++)
249 s
->z_err
= s
->z_eof
? Z_DATA_ERROR
: Z_OK
;
253 * new open(), close(), read(), lseek()
257 open(const char *fname
, int mode
)
262 if (((fd
= oopen(fname
, mode
)) == -1) || (mode
!= 0))
263 /* compression only for read */
266 ss
[fd
] = s
= alloc(sizeof(struct sd
));
269 (void)memset(s
, 0, sizeof(struct sd
));
271 if (inflateInit2(&(s
->stream
), -15) != Z_OK
)
274 s
->stream
.next_in
= s
->inbuf
= (unsigned char *)alloc(Z_BUFSIZE
);
276 inflateEnd(&(s
->stream
));
281 check_header(s
); /* skip the .gz header */
286 dealloc(s
, sizeof(struct sd
));
297 #if !defined(LIBSA_NO_FD_CHECKING)
298 if ((unsigned int)fd
>= SOPEN_MAX
) {
307 inflateEnd(&(s
->stream
));
309 dealloc(s
->inbuf
, Z_BUFSIZE
);
310 dealloc(s
, sizeof(struct sd
));
317 read(int fd
, void *buf
, size_t len
)
320 unsigned char *start
= buf
; /* starting point for crc computation */
324 if (s
->z_err
== Z_DATA_ERROR
|| s
->z_err
== Z_ERRNO
)
326 if (s
->z_err
== Z_STREAM_END
)
329 s
->stream
.next_out
= buf
;
330 s
->stream
.avail_out
= len
;
332 while (s
->stream
.avail_out
!= 0) {
334 if (s
->compressed
== 0) {
335 /* Copy first the lookahead bytes: */
336 unsigned int n
= s
->stream
.avail_in
;
337 if (n
> s
->stream
.avail_out
)
338 n
= s
->stream
.avail_out
;
340 zmemcpy(s
->stream
.next_out
,
341 s
->stream
.next_in
, n
);
342 s
->stream
.next_out
+= n
;
343 s
->stream
.next_in
+= n
;
344 s
->stream
.avail_out
-= n
;
345 s
->stream
.avail_in
-= n
;
347 if (s
->stream
.avail_out
> 0) {
349 got
= oread(s
->fd
, s
->stream
.next_out
,
350 s
->stream
.avail_out
);
353 s
->stream
.avail_out
-= got
;
355 return (int)(len
- s
->stream
.avail_out
);
358 if (s
->stream
.avail_in
== 0 && !s
->z_eof
) {
361 got
= oread(fd
, s
->inbuf
, Z_BUFSIZE
);
369 s
->stream
.avail_in
= got
;
370 s
->stream
.next_in
= s
->inbuf
;
373 s
->z_err
= inflate(&(s
->stream
), Z_NO_FLUSH
);
375 if (s
->z_err
== Z_STREAM_END
) {
376 /* Check CRC and original size */
377 s
->crc
= crc32(s
->crc
, start
, (unsigned int)
378 (s
->stream
.next_out
- start
));
379 start
= s
->stream
.next_out
;
381 if (getLong(s
) != s
->crc
||
382 getLong(s
) != s
->stream
.total_out
) {
384 s
->z_err
= Z_DATA_ERROR
;
386 /* Check for concatenated .gz files: */
388 if (s
->z_err
== Z_OK
) {
389 inflateReset(&(s
->stream
));
390 s
->crc
= crc32(0L, Z_NULL
, 0);
394 if (s
->z_err
!= Z_OK
|| s
->z_eof
)
398 s
->crc
= crc32(s
->crc
, start
,
399 (unsigned int)(s
->stream
.next_out
- start
));
401 return (int)(len
- s
->stream
.avail_out
);
405 lseek(int fd
, off_t offset
, int where
)
410 #if !defined(LIBSA_NO_FD_CHECKING)
411 if ((unsigned int)fd
>= SOPEN_MAX
) {
418 if ((f
->f_flags
& F_READ
) == 0)
419 return olseek(fd
, offset
, where
);
423 if(s
->compressed
== 0) {
424 off_t res
= olseek(fd
, offset
, where
);
425 if (res
!= (off_t
)-1) {
426 /* make sure the lookahead buffer is invalid */
427 s
->stream
.avail_in
= 0;
434 offset
+= s
->stream
.total_out
;
436 /* if seek backwards, simply start from the beginning */
437 if (offset
< s
->stream
.total_out
) {
441 res
= olseek(fd
, 0, SEEK_SET
);
444 /* ??? perhaps fallback to close / open */
446 inflateEnd(&(s
->stream
));
448 sav_inbuf
= s
->inbuf
; /* don't allocate again */
449 (void)memset(s
, 0, sizeof(struct sd
));
450 /* this resets total_out to 0! */
452 inflateInit2(&(s
->stream
), -15);
453 s
->stream
.next_in
= s
->inbuf
= sav_inbuf
;
456 check_header(s
); /* skip the .gz header */
459 /* to seek forwards, throw away data */
460 if (offset
> s
->stream
.total_out
) {
461 off_t toskip
= offset
- s
->stream
.total_out
;
464 #define DUMMYBUFSIZE 256
465 char dummybuf
[DUMMYBUFSIZE
];
468 if (len
> DUMMYBUFSIZE
)
470 if (read(fd
, dummybuf
, len
) != len
) {
478 if (offset
!= s
->stream
.total_out
)
479 panic("lseek compressed");