Home | History | Annotate | Line # | Download | only in hpcboot
file_manager.cpp revision 1.6.4.1
      1 /* -*-C++-*-	$NetBSD: file_manager.cpp,v 1.6.4.1 2006/09/09 02:39:27 rpaulo 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, uint32_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 	uint8_t *start = reinterpret_cast<uint8_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<uint8_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 			uint32_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 				_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 
    243 	if (!_compressed) {
    244 		_file->seek(offset);
    245 		_stream->avail_in = 0;
    246 
    247 		return TRUE;
    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 
    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 uint32_t
    299 FileManager::_get_long()
    300 {
    301 	uint32_t x = static_cast<uint32_t>(_get_byte());
    302 	int c;
    303 
    304 	x +=(static_cast<uint32_t>(_get_byte())) << 8;
    305 	x +=(static_cast<uint32_t>(_get_byte())) << 16;
    306 	c = _get_byte();
    307 	if (c == EOF)
    308 		_z_err = Z_DATA_ERROR;
    309 	x +=(static_cast<uint32_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