1 /* -*-C++-*-	$NetBSD: file_manager.cpp,v 1.2 2001/05/08 18:51:22 uch Exp $	*/
2 
3 /*-
4  * Copyright(c) 1996, 2001 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 
42 __BEGIN_DECLS
43 #include <string.h>
44 #include <zlib.h>
45 __END_DECLS
46 
47 static struct z_stream_s __stream;	// XXX for namespace.
48 
49 void
50 FileManager::_reset()
51 {
52 	_stream = &__stream;
53 	memset(_stream, 0, sizeof(struct z_stream_s));
54 	_z_err = 0;
55 	_z_eof = 0;
56 	_crc = 0;
57 	_compressed = 0;
58 }
59 
60 FileManager::~FileManager()
61 {
62 	delete _file;
63 }
64 
65 BOOL
66 FileManager::setRoot(TCHAR *drive)
67 {
68 	return _file->setRoot(drive);
69 }
70 
71 BOOL
72 FileManager::open(const TCHAR *name, u_int32_t flags)
73 {
74 	if (!_file->open(name, flags))
75 		return FALSE;
76 
77 	_reset();
78 
79 	if (inflateInit2(_stream, -15) != Z_OK)
80 		goto errout;
81 	_stream->next_in = _inbuf;
82 
83 	_check_header(); // skip the .gz header
84 
85 	return TRUE;
86  errout:
87 	_file->close();
88 	return FALSE;
89 }
90 
91 size_t
92 FileManager::read(void *buf, size_t len, off_t ofs)
93 {
94 	if (ofs != -1)
95 		seek(ofs);
96 
97 	return _read(buf, len);
98 }
99 
100 size_t
101 FileManager::_read(void *buf, size_t len)
102 {
103 	// starting point for crc computation
104 	u_int8_t *start = reinterpret_cast<u_int8_t *>(buf);
105 
106 	if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
107 		return -1;
108 	}
109 	if (_z_err == Z_STREAM_END) {
110 		return 0;  // EOF
111 	}
112 	_stream->next_out = reinterpret_cast<u_int8_t *>(buf);
113 	_stream->avail_out = len;
114 
115 	int got;
116 	while (_stream->avail_out != 0) {
117 		if (!_compressed) {
118 			// Copy first the lookahead bytes
119 			u_int32_t n = _stream->avail_in;
120 			if (n > _stream->avail_out)
121 				n = _stream->avail_out;
122 			if (n > 0) {
123 				memcpy(_stream->next_out, _stream->next_in, n);
124 				_stream->next_out  += n;
125 				_stream->next_in   += n;
126 				_stream->avail_out -= n;
127 				_stream->avail_in  -= n;
128 			}
129 			if (_stream->avail_out > 0) {
130 				got = _file->read(_stream->next_out,
131 				    _stream->avail_out);
132 				if (got == -1) {
133 					return(got);
134 				}
135 				_stream->avail_out -= got;
136 			}
137 			return(int)(len - _stream->avail_out);
138 		}
139 
140 		if (_stream->avail_in == 0 && !_z_eof) {
141 			got = _file->read(_inbuf, Z_BUFSIZE);
142 			if (got <= 0)
143 				_z_eof = 1;
144 
145 			_stream->avail_in = got;
146 			_stream->next_in = _inbuf;
147 		}
148 
149 		_z_err = inflate(_stream, Z_NO_FLUSH);
150 
151 		if (_z_err == Z_STREAM_END) {
152 			/* Check CRC and original size */
153 			_crc = crc32(_crc, start,(unsigned int)
154 			    (_stream->next_out - start));
155 			start = _stream->next_out;
156 
157 			if (_get_long() != _crc ||
158 			    _get_long() != _stream->total_out) {
159 
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 BOOL
200 FileManager::seek(off_t offset)
201 {
202 	if (!_compressed) {
203 		_file->seek(offset);
204 		_stream->avail_in = 0;
205 
206 		return TRUE;
207 	}
208 
209 	/* if seek backwards, simply start from the beginning */
210 	if (offset < _stream->total_out) {
211 		_file->seek(0);
212 
213 		inflateEnd(_stream);
214 		_reset(); /* this resets total_out to 0! */
215 		inflateInit2(_stream, -15);
216 		_stream->next_in = _inbuf;
217 
218 		_check_header(); /* skip the .gz header */
219 	}
220 
221 	/* to seek forwards, throw away data */
222 	if (offset > _stream->total_out) {
223 		off_t toskip = offset - _stream->total_out;
224 
225 		while (toskip > 0) {
226 #define DUMMYBUFSIZE 256
227 			char dummybuf[DUMMYBUFSIZE];
228 			off_t len = toskip;
229 			if (len > DUMMYBUFSIZE)
230 				len = DUMMYBUFSIZE;
231 			if (_read(dummybuf, len) != len)
232 				return FALSE;
233 			toskip -= len;
234 		}
235 	}
236 
237 	return TRUE;
238 }
239 
240 //
241 // GZIP util.
242 //
243 int
244 FileManager::_get_byte()
245 {
246 	if (_z_eof)
247 		return(EOF);
248 
249 	if (_stream->avail_in == 0) {
250 		int got;
251 
252 		got = _file->read(_inbuf, Z_BUFSIZE);
253 		if (got <= 0) {
254 			_z_eof = 1;
255 			return EOF;
256 		}
257 		_stream->avail_in = got;
258 		_stream->next_in = _inbuf;
259 	}
260 	_stream->avail_in--;
261 	return *(_stream->next_in)++;
262 }
263 
264 u_int32_t
265 FileManager::_get_long()
266 {
267 	u_int32_t x = static_cast<u_int32_t>(_get_byte());
268 	int c;
269 
270 	x +=(static_cast<u_int32_t>(_get_byte())) << 8;
271 	x +=(static_cast<u_int32_t>(_get_byte())) << 16;
272 	c = _get_byte();
273 	if (c == EOF)
274 		_z_err = Z_DATA_ERROR;
275 	x +=(static_cast<u_int32_t>(c)) << 24;
276 
277 	return x;
278 }
279 
280 void
281 FileManager::_check_header()
282 {
283 	int method; /* method byte */
284 	int flags;  /* flags byte */
285 	unsigned int len;
286 	int c;
287 
288 	/* Check the gzip magic header */
289 	for (len = 0; len < 2; len++) {
290 		c = _get_byte();
291 		if (c == _gz_magic[len])
292 			continue;
293 		if ((c == EOF) &&(len == 0))  {
294 			/*
295 			 * We must not change _compressed if we are at EOF;
296 			 * we may have come to the end of a gzipped file and be
297 			 * check to see if another gzipped file is concatenated
298 			 * to this one. If one isn't, we still need to be able
299 			 * to lseek on this file as a compressed file.
300 			 */
301 			return;
302 		}
303 		_compressed = 0;
304 		if (c != EOF) {
305 			_stream->avail_in++;
306 			_stream->next_in--;
307 		}
308 		_z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
309 		return;
310 	}
311 	_compressed = 1;
312 	method = _get_byte();
313 	flags = _get_byte();
314 	if (method != Z_DEFLATED ||(flags & RESERVED) != 0) {
315 		_z_err = Z_DATA_ERROR;
316 		return;
317 	}
318 
319 	/* Discard time, xflags and OS code: */
320 	for (len = 0; len < 6; len++)
321 		(void)_get_byte();
322 
323 	if ((flags & EXTRA_FIELD) != 0) {
324 		/* skip the extra field */
325 		len  = (unsigned int)_get_byte();
326 		len +=((unsigned int)_get_byte()) << 8;
327 		/* len is garbage if EOF but the loop below will quit anyway */
328 		while (len-- != 0 && _get_byte() != EOF) /*void*/;
329 	}
330 	if ((flags & ORIG_NAME) != 0) {
331 		/* skip the original file name */
332 		while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
333 	}
334 	if ((flags & COMMENT) != 0) {
335 		/* skip the .gz file comment */
336 		while ((c = _get_byte()) != 0 && c != EOF) /*void*/;
337 	}
338 	if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
339 		for (len = 0; len < 2; len++)
340 			(void)_get_byte();
341 	}
342 	_z_err = _z_eof ? Z_DATA_ERROR : Z_OK;
343 }
344