Home | History | Annotate | Line # | Download | only in hpcboot
file_manager.cpp revision 1.7.70.1
      1 /* -*-C++-*-	$NetBSD: file_manager.cpp,v 1.7.70.1 2008/05/18 12:31:59 yamt 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  *
     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.
     30  */
     31 
     32 #include <console.h>
     33 #include <file.h>
     34 #include <limits.h>
     35 
     36 __BEGIN_DECLS
     37 #include <string.h>
     38 #include <zlib.h>
     39 __END_DECLS
     40 
     41 static struct z_stream_s __stream;	// XXX for namespace.
     42 
     43 void
     44 FileManager::_reset()
     45 {
     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;
     52 }
     53 
     54 FileManager::~FileManager()
     55 {
     56 	delete _file;
     57 }
     58 
     59 BOOL
     60 FileManager::setRoot(TCHAR *drive)
     61 {
     62 	return _file->setRoot(drive);
     63 }
     64 
     65 BOOL
     66 FileManager::open(const TCHAR *name, uint32_t flags)
     67 {
     68 	if (!_file->open(name, flags))
     69 		return FALSE;
     70 
     71 	_reset();
     72 
     73 	if (inflateInit2(_stream, -15) != Z_OK)
     74 		goto errout;
     75 	_stream->next_in = _inbuf;
     76 
     77 	_check_header(); // skip the .gz header
     78 
     79 	return TRUE;
     80  errout:
     81 	_file->close();
     82 	return FALSE;
     83 }
     84 
     85 size_t
     86 FileManager::read(void *buf, size_t len, off_t ofs)
     87 {
     88 	if (ofs != -1)
     89 		seek(ofs);
     90 
     91 	return _read(buf, len);
     92 }
     93 
     94 size_t
     95 FileManager::_read(void *buf, size_t len)
     96 {
     97 	// starting point for crc computation
     98 	uint8_t *start = reinterpret_cast<uint8_t *>(buf);
     99 
    100 	if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
    101 		return -1;
    102 	}
    103 	if (_z_err == Z_STREAM_END) {
    104 		return 0;  // EOF
    105 	}
    106 	_stream->next_out = reinterpret_cast<uint8_t *>(buf);
    107 	_stream->avail_out = len;
    108 
    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;
    122 			}
    123 			if (_stream->avail_out > 0) {
    124 				got = _file->read(_stream->next_out,
    125 				    _stream->avail_out);
    126 				if (got == -1) {
    127 					return(got);
    128 				}
    129 				_stream->avail_out -= got;
    130 			}
    131 			return(int)(len - _stream->avail_out);
    132 		}
    133 
    134 		if (_stream->avail_in == 0 && !_z_eof) {
    135 			got = _file->read(_inbuf, Z_BUFSIZE);
    136 			if (got <= 0)
    137 				_z_eof = 1;
    138 
    139 			_stream->avail_in = got;
    140 			_stream->next_in = _inbuf;
    141 		}
    142 
    143 		_z_err = inflate(_stream, Z_NO_FLUSH);
    144 
    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;
    150 
    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);
    160 				}
    161 			}
    162 		}
    163 		if (_z_err != Z_OK || _z_eof)
    164 			break;
    165 	}
    166 
    167 	_crc = crc32(_crc, start,(unsigned int)(_stream->next_out - start));
    168 
    169 	return(int)(len - _stream->avail_out);
    170 }
    171 
    172 size_t
    173 FileManager::write(const void *buf, size_t bytes, off_t ofs)
    174 {
    175 	return _file->write(buf, bytes, ofs);
    176 }
    177 
    178 size_t
    179 FileManager::size()
    180 {
    181 	return _file->size();
    182 }
    183 
    184 BOOL
    185 FileManager::close()
    186 {
    187 	inflateEnd(_stream);
    188 
    189 	return _file->close();
    190 }
    191 
    192 size_t
    193 FileManager::_skip_compressed(off_t toskip)
    194 {
    195 #define	DUMMYBUFSIZE 256
    196 	char dummybuf[DUMMYBUFSIZE];
    197 
    198 	size_t skipped = 0;
    199 
    200 	while (toskip > 0) {
    201 		size_t toread = toskip;
    202 		if (toread > DUMMYBUFSIZE)
    203 			toread = DUMMYBUFSIZE;
    204 
    205 		size_t nread = _read(dummybuf, toread);
    206 		if ((int)nread < 0)
    207 			return nread;
    208 
    209 		toskip  -= nread;
    210 		skipped += nread;
    211 
    212 		if (nread != toread)
    213 			break;
    214 	}
    215 
    216 	return skipped;
    217 }
    218 
    219 size_t
    220 FileManager::realsize()
    221 {
    222 	if (!_compressed)
    223 		return size();
    224 
    225 	off_t pos = _stream->total_out;
    226 	size_t sz = _skip_compressed(INT_MAX);
    227 	seek(pos);
    228 
    229 	return sz;
    230 }
    231 
    232 BOOL
    233 FileManager::seek(off_t offset)
    234 {
    235 
    236 	if (!_compressed) {
    237 		_file->seek(offset);
    238 		_stream->avail_in = 0;
    239 
    240 		return TRUE;
    241 	}
    242 	/* if seek backwards, simply start from the beginning */
    243 	if (offset < _stream->total_out) {
    244 		_file->seek(0);
    245 
    246 		inflateEnd(_stream);
    247 		_reset(); /* this resets total_out to 0! */
    248 		inflateInit2(_stream, -15);
    249 		_stream->next_in = _inbuf;
    250 
    251 		_check_header(); /* skip the .gz header */
    252 	}
    253 
    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);
    258 
    259 		if (skipped != toskip)
    260 			return FALSE;
    261 	}
    262 
    263 	return TRUE;
    264 }
    265 
    266 //
    267 // GZIP util.
    268 //
    269 int
    270 FileManager::_get_byte()
    271 {
    272 
    273 	if (_z_eof)
    274 		return(EOF);
    275 
    276 	if (_stream->avail_in == 0) {
    277 		int got;
    278 
    279 		got = _file->read(_inbuf, Z_BUFSIZE);
    280 		if (got <= 0) {
    281 			_z_eof = 1;
    282 			return EOF;
    283 		}
    284 		_stream->avail_in = got;
    285 		_stream->next_in = _inbuf;
    286 	}
    287 	_stream->avail_in--;
    288 	return *(_stream->next_in)++;
    289 }
    290 
    291 uint32_t
    292 FileManager::_get_long()
    293 {
    294 	uint32_t x = static_cast<uint32_t>(_get_byte());
    295 	int c;
    296 
    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;
    303 
    304 	return x;
    305 }
    306 
    307 void
    308 FileManager::_check_header()
    309 {
    310 	int method; /* method byte */
    311 	int flags;  /* flags byte */
    312 	unsigned int len;
    313 	int c;
    314 
    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))  {
    321 			/*
    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.
    327 			 */
    328 			return;
    329 		}
    330 		_compressed = 0;
    331 		if (c != EOF) {
    332 			_stream->avail_in++;
    333 			_stream->next_in--;
    334 		}
    335 		_z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
    336 		return;
    337 	}
    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;
    344 	}
    345 
    346 	/* Discard time, xflags and OS code: */
    347 	for (len = 0; len < 6; len++)
    348 		(void)_get_byte();
    349 
    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*/;
    356 	}
    357 	if ((flags & ORIG_NAME) != 0) {
    358 		/* skip the original file name */
    359 		while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
    360 	}
    361 	if ((flags & COMMENT) != 0) {
    362 		/* skip the .gz file comment */
    363 		while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
    364 	}
    365 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
    366 		for (len = 0; len < 2; len++)
    367 			(void)_get_byte();
    368 	}
    369 	_z_err = _z_eof ? Z_DATA_ERROR : Z_OK;
    370 }
    371