file_manager.cpp revision 1.4 1 /* -*-C++-*- $NetBSD: file_manager.cpp,v 1.4 2004/08/06 17:21:28 uch Exp $ */
2
3 /*-
4 * Copyright(c) 1996, 2001, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Drochner. and UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <console.h>
40 #include <file.h>
41 #include <limits.h>
42
43 __BEGIN_DECLS
44 #include <string.h>
45 #include <zlib.h>
46 __END_DECLS
47
48 static struct z_stream_s __stream; // XXX for namespace.
49
50 void
51 FileManager::_reset()
52 {
53 _stream = &__stream;
54 memset(_stream, 0, sizeof(struct z_stream_s));
55 _z_err = 0;
56 _z_eof = 0;
57 _crc = 0;
58 _compressed = 0;
59 }
60
61 FileManager::~FileManager()
62 {
63 delete _file;
64 }
65
66 BOOL
67 FileManager::setRoot(TCHAR *drive)
68 {
69 return _file->setRoot(drive);
70 }
71
72 BOOL
73 FileManager::open(const TCHAR *name, u_int32_t flags)
74 {
75 if (!_file->open(name, flags))
76 return FALSE;
77
78 _reset();
79
80 if (inflateInit2(_stream, -15) != Z_OK)
81 goto errout;
82 _stream->next_in = _inbuf;
83
84 _check_header(); // skip the .gz header
85
86 return TRUE;
87 errout:
88 _file->close();
89 return FALSE;
90 }
91
92 size_t
93 FileManager::read(void *buf, size_t len, off_t ofs)
94 {
95 if (ofs != -1)
96 seek(ofs);
97
98 return _read(buf, len);
99 }
100
101 size_t
102 FileManager::_read(void *buf, size_t len)
103 {
104 // starting point for crc computation
105 u_int8_t *start = reinterpret_cast<u_int8_t *>(buf);
106
107 if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
108 return -1;
109 }
110 if (_z_err == Z_STREAM_END) {
111 return 0; // EOF
112 }
113 _stream->next_out = reinterpret_cast<u_int8_t *>(buf);
114 _stream->avail_out = len;
115
116 int got;
117 while (_stream->avail_out != 0) {
118 if (!_compressed) {
119 // Copy first the lookahead bytes
120 u_int32_t n = _stream->avail_in;
121 if (n > _stream->avail_out)
122 n = _stream->avail_out;
123 if (n > 0) {
124 memcpy(_stream->next_out, _stream->next_in, n);
125 _stream->next_out += n;
126 _stream->next_in += n;
127 _stream->avail_out -= n;
128 _stream->avail_in -= n;
129 }
130 if (_stream->avail_out > 0) {
131 got = _file->read(_stream->next_out,
132 _stream->avail_out);
133 if (got == -1) {
134 return(got);
135 }
136 _stream->avail_out -= got;
137 }
138 return(int)(len - _stream->avail_out);
139 }
140
141 if (_stream->avail_in == 0 && !_z_eof) {
142 got = _file->read(_inbuf, Z_BUFSIZE);
143 if (got <= 0)
144 _z_eof = 1;
145
146 _stream->avail_in = got;
147 _stream->next_in = _inbuf;
148 }
149
150 _z_err = inflate(_stream, Z_NO_FLUSH);
151
152 if (_z_err == Z_STREAM_END) {
153 /* Check CRC and original size */
154 _crc = crc32(_crc, start,(unsigned int)
155 (_stream->next_out - start));
156 start = _stream->next_out;
157
158 if (_get_long() != _crc ||
159 _get_long() != _stream->total_out) {
160
161 _z_err = Z_DATA_ERROR;
162 } else {
163 /* Check for concatenated .gz files: */
164 _check_header();
165 if (_z_err == Z_OK) {
166 inflateReset(_stream);
167 _crc = crc32(0L, Z_NULL, 0);
168 }
169 }
170 }
171 if (_z_err != Z_OK || _z_eof)
172 break;
173 }
174
175 _crc = crc32(_crc, start,(unsigned int)(_stream->next_out - start));
176
177 return(int)(len - _stream->avail_out);
178 }
179
180 size_t
181 FileManager::write(const void *buf, size_t bytes, off_t ofs)
182 {
183 return _file->write(buf, bytes, ofs);
184 }
185
186 size_t
187 FileManager::size()
188 {
189 return _file->size();
190 }
191
192 BOOL
193 FileManager::close()
194 {
195 inflateEnd(_stream);
196
197 return _file->close();
198 }
199
200 size_t
201 FileManager::_skip_compressed(off_t toskip)
202 {
203 #define DUMMYBUFSIZE 256
204 char dummybuf[DUMMYBUFSIZE];
205
206 size_t skipped = 0;
207
208 while (toskip > 0) {
209 size_t toread = toskip;
210 if (toread > DUMMYBUFSIZE)
211 toread = DUMMYBUFSIZE;
212
213 size_t nread = _read(dummybuf, toread);
214 if ((int)nread < 0)
215 return nread;
216
217 toskip -= nread;
218 skipped += nread;
219
220 if (nread != toread)
221 break;
222 }
223
224 return skipped;
225 }
226
227 size_t
228 FileManager::realsize()
229 {
230 if (!_compressed)
231 return size();
232
233 off_t pos = _stream->total_out;
234 size_t sz = _skip_compressed(INT_MAX);
235 seek(pos);
236
237 return sz;
238 }
239
240 BOOL
241 FileManager::seek(off_t offset)
242 {
243 if (!_compressed) {
244 _file->seek(offset);
245 _stream->avail_in = 0;
246
247 return TRUE;
248 }
249
250 /* if seek backwards, simply start from the beginning */
251 if (offset < _stream->total_out) {
252 _file->seek(0);
253
254 inflateEnd(_stream);
255 _reset(); /* this resets total_out to 0! */
256 inflateInit2(_stream, -15);
257 _stream->next_in = _inbuf;
258
259 _check_header(); /* skip the .gz header */
260 }
261
262 /* to seek forwards, throw away data */
263 if (offset > _stream->total_out) {
264 off_t toskip = offset - _stream->total_out;
265 size_t skipped = _skip_compressed(toskip);
266
267 if (skipped != toskip)
268 return FALSE;
269 }
270
271 return TRUE;
272 }
273
274 //
275 // GZIP util.
276 //
277 int
278 FileManager::_get_byte()
279 {
280 if (_z_eof)
281 return(EOF);
282
283 if (_stream->avail_in == 0) {
284 int got;
285
286 got = _file->read(_inbuf, Z_BUFSIZE);
287 if (got <= 0) {
288 _z_eof = 1;
289 return EOF;
290 }
291 _stream->avail_in = got;
292 _stream->next_in = _inbuf;
293 }
294 _stream->avail_in--;
295 return *(_stream->next_in)++;
296 }
297
298 u_int32_t
299 FileManager::_get_long()
300 {
301 u_int32_t x = static_cast<u_int32_t>(_get_byte());
302 int c;
303
304 x +=(static_cast<u_int32_t>(_get_byte())) << 8;
305 x +=(static_cast<u_int32_t>(_get_byte())) << 16;
306 c = _get_byte();
307 if (c == EOF)
308 _z_err = Z_DATA_ERROR;
309 x +=(static_cast<u_int32_t>(c)) << 24;
310
311 return x;
312 }
313
314 void
315 FileManager::_check_header()
316 {
317 int method; /* method byte */
318 int flags; /* flags byte */
319 unsigned int len;
320 int c;
321
322 /* Check the gzip magic header */
323 for (len = 0; len < 2; len++) {
324 c = _get_byte();
325 if (c == _gz_magic[len])
326 continue;
327 if ((c == EOF) &&(len == 0)) {
328 /*
329 * We must not change _compressed if we are at EOF;
330 * we may have come to the end of a gzipped file and be
331 * check to see if another gzipped file is concatenated
332 * to this one. If one isn't, we still need to be able
333 * to lseek on this file as a compressed file.
334 */
335 return;
336 }
337 _compressed = 0;
338 if (c != EOF) {
339 _stream->avail_in++;
340 _stream->next_in--;
341 }
342 _z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
343 return;
344 }
345 _compressed = 1;
346 method = _get_byte();
347 flags = _get_byte();
348 if (method != Z_DEFLATED ||(flags & RESERVED) != 0) {
349 _z_err = Z_DATA_ERROR;
350 return;
351 }
352
353 /* Discard time, xflags and OS code: */
354 for (len = 0; len < 6; len++)
355 (void)_get_byte();
356
357 if ((flags & EXTRA_FIELD) != 0) {
358 /* skip the extra field */
359 len = (unsigned int)_get_byte();
360 len +=((unsigned int)_get_byte()) << 8;
361 /* len is garbage if EOF but the loop below will quit anyway */
362 while (len-- != 0 && _get_byte() != EOF) /*void*/;
363 }
364 if ((flags & ORIG_NAME) != 0) {
365 /* skip the original file name */
366 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
367 }
368 if ((flags & COMMENT) != 0) {
369 /* skip the .gz file comment */
370 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
371 }
372 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
373 for (len = 0; len < 2; len++)
374 (void)_get_byte();
375 }
376 _z_err = _z_eof ? Z_DATA_ERROR : Z_OK;
377 }
378