1 /* ----------------------------------------------------------------------- *
3 * Copyright 2003-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
33 #include <syslinux/zio.h>
41 * Open an ordinary file, possibly compressed; if so, insert
42 * an appropriate decompressor.
45 int __file_get_block(struct file_info
*fp
);
46 int __file_close(struct file_info
*fp
);
48 static ssize_t
gzip_file_read(struct file_info
*, void *, size_t);
49 static int gzip_file_close(struct file_info
*);
51 static const struct input_dev gzip_file_dev
= {
52 .dev_magic
= __DEV_MAGIC
,
53 .flags
= __DEV_FILE
| __DEV_INPUT
,
54 .fileflags
= O_RDONLY
,
55 .read
= gzip_file_read
,
56 .close
= gzip_file_close
,
60 static int gzip_file_init(struct file_info
*fp
)
62 z_streamp zs
= calloc(1, sizeof(z_stream
));
69 zs
->next_in
= (void *)fp
->i
.datap
;
70 zs
->avail_in
= fp
->i
.nbytes
;
72 if (inflateInit2(zs
, 15 + 32) != Z_OK
) {
77 fp
->iop
= &gzip_file_dev
;
78 fp
->i
.fd
.size
= -1; /* Unknown */
83 static ssize_t
gzip_file_read(struct file_info
*fp
, void *ptr
, size_t n
)
85 z_streamp zs
= fp
->i
.pvt
;
89 unsigned char *p
= ptr
;
95 if (!zs
->avail_in
&& fp
->i
.fd
.handle
) {
96 if (__file_get_block(fp
))
97 return nout
? nout
: -1;
99 zs
->next_in
= (void *)fp
->i
.datap
;
100 zs
->avail_in
= fp
->i
.nbytes
;
103 rv
= inflate(zs
, Z_SYNC_FLUSH
);
105 bytes
= n
- zs
->avail_out
;
117 return nout
? nout
: -1;
120 return nout
? nout
: -1;
131 static int gzip_file_close(struct file_info
*fp
)
133 z_streamp zs
= fp
->i
.pvt
;
137 return __file_close(fp
);
140 int zopen(const char *pathname
, int flags
, ...)
143 struct file_info
*fp
;
145 /* We don't actually give a hoot about the creation bits... */
146 fd
= open(pathname
, flags
, 0);
151 fp
= &__file_info
[fd
];
153 /* Need to get the first block into the buffer, but not consumed */
154 if (__file_get_block(fp
))
157 if (fp
->i
.nbytes
>= 14 &&
158 (uint8_t) fp
->i
.buf
[0] == 037 &&
159 (uint8_t) fp
->i
.buf
[1] == 0213 && /* gzip */
160 fp
->i
.buf
[2] == 8) /* deflate */
161 rv
= gzip_file_init(fp
);
163 rv
= 0; /* Plain file */