file_manager.cpp revision 1.2.24.1 1 /* -*-C++-*- $NetBSD: file_manager.cpp,v 1.2.24.1 2004/08/03 10:34:58 skrll Exp $ */
2
3 /*-
4 * Copyright(c) 1996, 2001 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
42 __BEGIN_DECLS
43 #include <string.h>
44 #include <zlib.h>
45 __END_DECLS
46
47 static struct z_stream_s __stream; // XXX for namespace.
48
49 void
50 FileManager::_reset()
51 {
52 _stream = &__stream;
53 memset(_stream, 0, sizeof(struct z_stream_s));
54 _z_err = 0;
55 _z_eof = 0;
56 _crc = 0;
57 _compressed = 0;
58 }
59
60 FileManager::~FileManager()
61 {
62 delete _file;
63 }
64
65 BOOL
66 FileManager::setRoot(TCHAR *drive)
67 {
68 return _file->setRoot(drive);
69 }
70
71 BOOL
72 FileManager::open(const TCHAR *name, u_int32_t flags)
73 {
74 if (!_file->open(name, flags))
75 return FALSE;
76
77 _reset();
78
79 if (inflateInit2(_stream, -15) != Z_OK)
80 goto errout;
81 _stream->next_in = _inbuf;
82
83 _check_header(); // skip the .gz header
84
85 return TRUE;
86 errout:
87 _file->close();
88 return FALSE;
89 }
90
91 size_t
92 FileManager::read(void *buf, size_t len, off_t ofs)
93 {
94 if (ofs != -1)
95 seek(ofs);
96
97 return _read(buf, len);
98 }
99
100 size_t
101 FileManager::_read(void *buf, size_t len)
102 {
103 // starting point for crc computation
104 u_int8_t *start = reinterpret_cast<u_int8_t *>(buf);
105
106 if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
107 return -1;
108 }
109 if (_z_err == Z_STREAM_END) {
110 return 0; // EOF
111 }
112 _stream->next_out = reinterpret_cast<u_int8_t *>(buf);
113 _stream->avail_out = len;
114
115 int got;
116 while (_stream->avail_out != 0) {
117 if (!_compressed) {
118 // Copy first the lookahead bytes
119 u_int32_t n = _stream->avail_in;
120 if (n > _stream->avail_out)
121 n = _stream->avail_out;
122 if (n > 0) {
123 memcpy(_stream->next_out, _stream->next_in, n);
124 _stream->next_out += n;
125 _stream->next_in += n;
126 _stream->avail_out -= n;
127 _stream->avail_in -= n;
128 }
129 if (_stream->avail_out > 0) {
130 got = _file->read(_stream->next_out,
131 _stream->avail_out);
132 if (got == -1) {
133 return(got);
134 }
135 _stream->avail_out -= got;
136 }
137 return(int)(len - _stream->avail_out);
138 }
139
140 if (_stream->avail_in == 0 && !_z_eof) {
141 got = _file->read(_inbuf, Z_BUFSIZE);
142 if (got <= 0)
143 _z_eof = 1;
144
145 _stream->avail_in = got;
146 _stream->next_in = _inbuf;
147 }
148
149 _z_err = inflate(_stream, Z_NO_FLUSH);
150
151 if (_z_err == Z_STREAM_END) {
152 /* Check CRC and original size */
153 _crc = crc32(_crc, start,(unsigned int)
154 (_stream->next_out - start));
155 start = _stream->next_out;
156
157 if (_get_long() != _crc ||
158 _get_long() != _stream->total_out) {
159
160 _z_err = Z_DATA_ERROR;
161 } else {
162 /* Check for concatenated .gz files: */
163 _check_header();
164 if (_z_err == Z_OK) {
165 inflateReset(_stream);
166 _crc = crc32(0L, Z_NULL, 0);
167 }
168 }
169 }
170 if (_z_err != Z_OK || _z_eof)
171 break;
172 }
173
174 _crc = crc32(_crc, start,(unsigned int)(_stream->next_out - start));
175
176 return(int)(len - _stream->avail_out);
177 }
178
179 size_t
180 FileManager::write(const void *buf, size_t bytes, off_t ofs)
181 {
182 return _file->write(buf, bytes, ofs);
183 }
184
185 size_t
186 FileManager::size()
187 {
188 return _file->size();
189 }
190
191 BOOL
192 FileManager::close()
193 {
194 inflateEnd(_stream);
195
196 return _file->close();
197 }
198
199 size_t
200 FileManager::_skip_compressed(off_t toskip)
201 {
202 #define DUMMYBUFSIZE 256
203 char dummybuf[DUMMYBUFSIZE];
204
205 size_t skipped = 0;
206
207 while (toskip > 0) {
208 size_t toread = toskip;
209 if (toread > DUMMYBUFSIZE)
210 toread = DUMMYBUFSIZE;
211
212 size_t nread = _read(dummybuf, toread);
213 if ((int)nread < 0)
214 return nread;
215
216 toskip -= nread;
217 skipped += nread;
218
219 if (nread != toread)
220 break;
221 }
222
223 return skipped;
224 }
225
226 size_t
227 FileManager::realsize()
228 {
229 if (!_compressed)
230 return size();
231
232 off_t pos = _stream->total_out;
233 size_t sz = _skip_compressed(INT_MAX);
234 seek(pos);
235
236 return sz;
237 }
238
239 BOOL
240 FileManager::seek(off_t offset)
241 {
242 if (!_compressed) {
243 _file->seek(offset);
244 _stream->avail_in = 0;
245
246 return TRUE;
247 }
248
249 /* if seek backwards, simply start from the beginning */
250 if (offset < _stream->total_out) {
251 _file->seek(0);
252
253 inflateEnd(_stream);
254 _reset(); /* this resets total_out to 0! */
255 inflateInit2(_stream, -15);
256 _stream->next_in = _inbuf;
257
258 _check_header(); /* skip the .gz header */
259 }
260
261 /* to seek forwards, throw away data */
262 if (offset > _stream->total_out) {
263 off_t toskip = offset - _stream->total_out;
264 size_t skipped = _skip_compressed(toskip);
265
266 if (skipped != toskip)
267 return FALSE;
268 }
269
270 return TRUE;
271 }
272
273 //
274 // GZIP util.
275 //
276 int
277 FileManager::_get_byte()
278 {
279 if (_z_eof)
280 return(EOF);
281
282 if (_stream->avail_in == 0) {
283 int got;
284
285 got = _file->read(_inbuf, Z_BUFSIZE);
286 if (got <= 0) {
287 _z_eof = 1;
288 return EOF;
289 }
290 _stream->avail_in = got;
291 _stream->next_in = _inbuf;
292 }
293 _stream->avail_in--;
294 return *(_stream->next_in)++;
295 }
296
297 u_int32_t
298 FileManager::_get_long()
299 {
300 u_int32_t x = static_cast<u_int32_t>(_get_byte());
301 int c;
302
303 x +=(static_cast<u_int32_t>(_get_byte())) << 8;
304 x +=(static_cast<u_int32_t>(_get_byte())) << 16;
305 c = _get_byte();
306 if (c == EOF)
307 _z_err = Z_DATA_ERROR;
308 x +=(static_cast<u_int32_t>(c)) << 24;
309
310 return x;
311 }
312
313 void
314 FileManager::_check_header()
315 {
316 int method; /* method byte */
317 int flags; /* flags byte */
318 unsigned int len;
319 int c;
320
321 /* Check the gzip magic header */
322 for (len = 0; len < 2; len++) {
323 c = _get_byte();
324 if (c == _gz_magic[len])
325 continue;
326 if ((c == EOF) &&(len == 0)) {
327 /*
328 * We must not change _compressed if we are at EOF;
329 * we may have come to the end of a gzipped file and be
330 * check to see if another gzipped file is concatenated
331 * to this one. If one isn't, we still need to be able
332 * to lseek on this file as a compressed file.
333 */
334 return;
335 }
336 _compressed = 0;
337 if (c != EOF) {
338 _stream->avail_in++;
339 _stream->next_in--;
340 }
341 _z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
342 return;
343 }
344 _compressed = 1;
345 method = _get_byte();
346 flags = _get_byte();
347 if (method != Z_DEFLATED ||(flags & RESERVED) != 0) {
348 _z_err = Z_DATA_ERROR;
349 return;
350 }
351
352 /* Discard time, xflags and OS code: */
353 for (len = 0; len < 6; len++)
354 (void)_get_byte();
355
356 if ((flags & EXTRA_FIELD) != 0) {
357 /* skip the extra field */
358 len = (unsigned int)_get_byte();
359 len +=((unsigned int)_get_byte()) << 8;
360 /* len is garbage if EOF but the loop below will quit anyway */
361 while (len-- != 0 && _get_byte() != EOF) /*void*/;
362 }
363 if ((flags & ORIG_NAME) != 0) {
364 /* skip the original file name */
365 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
366 }
367 if ((flags & COMMENT) != 0) {
368 /* skip the .gz file comment */
369 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
370 }
371 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
372 for (len = 0; len < 2; len++)
373 (void)_get_byte();
374 }
375 _z_err = _z_eof ? Z_DATA_ERROR : Z_OK;
376 }
377