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