No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / hpc / stand / hpcboot / file_manager.cpp
blob972ffd77823f82c6118af04b537236842b515de2
1 /* -*-C++-*- $NetBSD: file_manager.cpp,v 1.7 2006/03/05 04:05:39 uwe Exp $ */
3 /*-
4 * Copyright(c) 1996, 2001, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Drochner. and UCHIYAMA Yasushi.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <console.h>
33 #include <file.h>
34 #include <limits.h>
36 __BEGIN_DECLS
37 #include <string.h>
38 #include <zlib.h>
39 __END_DECLS
41 static struct z_stream_s __stream; // XXX for namespace.
43 void
44 FileManager::_reset()
46 _stream = &__stream;
47 memset(_stream, 0, sizeof(struct z_stream_s));
48 _z_err = 0;
49 _z_eof = 0;
50 _crc = 0;
51 _compressed = 0;
54 FileManager::~FileManager()
56 delete _file;
59 BOOL
60 FileManager::setRoot(TCHAR *drive)
62 return _file->setRoot(drive);
65 BOOL
66 FileManager::open(const TCHAR *name, uint32_t flags)
68 if (!_file->open(name, flags))
69 return FALSE;
71 _reset();
73 if (inflateInit2(_stream, -15) != Z_OK)
74 goto errout;
75 _stream->next_in = _inbuf;
77 _check_header(); // skip the .gz header
79 return TRUE;
80 errout:
81 _file->close();
82 return FALSE;
85 size_t
86 FileManager::read(void *buf, size_t len, off_t ofs)
88 if (ofs != -1)
89 seek(ofs);
91 return _read(buf, len);
94 size_t
95 FileManager::_read(void *buf, size_t len)
97 // starting point for crc computation
98 uint8_t *start = reinterpret_cast<uint8_t *>(buf);
100 if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
101 return -1;
103 if (_z_err == Z_STREAM_END) {
104 return 0; // EOF
106 _stream->next_out = reinterpret_cast<uint8_t *>(buf);
107 _stream->avail_out = len;
109 int got;
110 while (_stream->avail_out != 0) {
111 if (!_compressed) {
112 // Copy first the lookahead bytes
113 uint32_t n = _stream->avail_in;
114 if (n > _stream->avail_out)
115 n = _stream->avail_out;
116 if (n > 0) {
117 memcpy(_stream->next_out, _stream->next_in, n);
118 _stream->next_out += n;
119 _stream->next_in += n;
120 _stream->avail_out -= n;
121 _stream->avail_in -= n;
123 if (_stream->avail_out > 0) {
124 got = _file->read(_stream->next_out,
125 _stream->avail_out);
126 if (got == -1) {
127 return(got);
129 _stream->avail_out -= got;
131 return(int)(len - _stream->avail_out);
134 if (_stream->avail_in == 0 && !_z_eof) {
135 got = _file->read(_inbuf, Z_BUFSIZE);
136 if (got <= 0)
137 _z_eof = 1;
139 _stream->avail_in = got;
140 _stream->next_in = _inbuf;
143 _z_err = inflate(_stream, Z_NO_FLUSH);
145 if (_z_err == Z_STREAM_END) {
146 /* Check CRC and original size */
147 _crc = crc32(_crc, start,(unsigned int)
148 (_stream->next_out - start));
149 start = _stream->next_out;
151 if (_get_long() != _crc ||
152 _get_long() != _stream->total_out) {
153 _z_err = Z_DATA_ERROR;
154 } else {
155 /* Check for concatenated .gz files: */
156 _check_header();
157 if (_z_err == Z_OK) {
158 inflateReset(_stream);
159 _crc = crc32(0L, Z_NULL, 0);
163 if (_z_err != Z_OK || _z_eof)
164 break;
167 _crc = crc32(_crc, start,(unsigned int)(_stream->next_out - start));
169 return(int)(len - _stream->avail_out);
172 size_t
173 FileManager::write(const void *buf, size_t bytes, off_t ofs)
175 return _file->write(buf, bytes, ofs);
178 size_t
179 FileManager::size()
181 return _file->size();
184 BOOL
185 FileManager::close()
187 inflateEnd(_stream);
189 return _file->close();
192 size_t
193 FileManager::_skip_compressed(off_t toskip)
195 #define DUMMYBUFSIZE 256
196 char dummybuf[DUMMYBUFSIZE];
198 size_t skipped = 0;
200 while (toskip > 0) {
201 size_t toread = toskip;
202 if (toread > DUMMYBUFSIZE)
203 toread = DUMMYBUFSIZE;
205 size_t nread = _read(dummybuf, toread);
206 if ((int)nread < 0)
207 return nread;
209 toskip -= nread;
210 skipped += nread;
212 if (nread != toread)
213 break;
216 return skipped;
219 size_t
220 FileManager::realsize()
222 if (!_compressed)
223 return size();
225 off_t pos = _stream->total_out;
226 size_t sz = _skip_compressed(INT_MAX);
227 seek(pos);
229 return sz;
232 BOOL
233 FileManager::seek(off_t offset)
236 if (!_compressed) {
237 _file->seek(offset);
238 _stream->avail_in = 0;
240 return TRUE;
242 /* if seek backwards, simply start from the beginning */
243 if (offset < _stream->total_out) {
244 _file->seek(0);
246 inflateEnd(_stream);
247 _reset(); /* this resets total_out to 0! */
248 inflateInit2(_stream, -15);
249 _stream->next_in = _inbuf;
251 _check_header(); /* skip the .gz header */
254 /* to seek forwards, throw away data */
255 if (offset > _stream->total_out) {
256 off_t toskip = offset - _stream->total_out;
257 size_t skipped = _skip_compressed(toskip);
259 if (skipped != toskip)
260 return FALSE;
263 return TRUE;
267 // GZIP util.
270 FileManager::_get_byte()
273 if (_z_eof)
274 return(EOF);
276 if (_stream->avail_in == 0) {
277 int got;
279 got = _file->read(_inbuf, Z_BUFSIZE);
280 if (got <= 0) {
281 _z_eof = 1;
282 return EOF;
284 _stream->avail_in = got;
285 _stream->next_in = _inbuf;
287 _stream->avail_in--;
288 return *(_stream->next_in)++;
291 uint32_t
292 FileManager::_get_long()
294 uint32_t x = static_cast<uint32_t>(_get_byte());
295 int c;
297 x +=(static_cast<uint32_t>(_get_byte())) << 8;
298 x +=(static_cast<uint32_t>(_get_byte())) << 16;
299 c = _get_byte();
300 if (c == EOF)
301 _z_err = Z_DATA_ERROR;
302 x +=(static_cast<uint32_t>(c)) << 24;
304 return x;
307 void
308 FileManager::_check_header()
310 int method; /* method byte */
311 int flags; /* flags byte */
312 unsigned int len;
313 int c;
315 /* Check the gzip magic header */
316 for (len = 0; len < 2; len++) {
317 c = _get_byte();
318 if (c == _gz_magic[len])
319 continue;
320 if ((c == EOF) &&(len == 0)) {
322 * We must not change _compressed if we are at EOF;
323 * we may have come to the end of a gzipped file and be
324 * check to see if another gzipped file is concatenated
325 * to this one. If one isn't, we still need to be able
326 * to lseek on this file as a compressed file.
328 return;
330 _compressed = 0;
331 if (c != EOF) {
332 _stream->avail_in++;
333 _stream->next_in--;
335 _z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
336 return;
338 _compressed = 1;
339 method = _get_byte();
340 flags = _get_byte();
341 if (method != Z_DEFLATED ||(flags & RESERVED) != 0) {
342 _z_err = Z_DATA_ERROR;
343 return;
346 /* Discard time, xflags and OS code: */
347 for (len = 0; len < 6; len++)
348 (void)_get_byte();
350 if ((flags & EXTRA_FIELD) != 0) {
351 /* skip the extra field */
352 len = (unsigned int)_get_byte();
353 len +=((unsigned int)_get_byte()) << 8;
354 /* len is garbage if EOF but the loop below will quit anyway */
355 while (len-- != 0 && _get_byte() != EOF) /*void*/;
357 if ((flags & ORIG_NAME) != 0) {
358 /* skip the original file name */
359 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
361 if ((flags & COMMENT) != 0) {
362 /* skip the .gz file comment */
363 while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
365 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
366 for (len = 0; len < 2; len++)
367 (void)_get_byte();
369 _z_err = _z_eof ? Z_DATA_ERROR : Z_OK;