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