1 /* -*-C++-*- $NetBSD: file_manager.cpp,v 1.7 2006/03/05 04:05:39 uwe Exp $ */
4 * Copyright(c) 1996, 2001, 2004 The NetBSD Foundation, Inc.
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
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.
41 static struct z_stream_s __stream
; // XXX for namespace.
47 memset(_stream
, 0, sizeof(struct z_stream_s
));
54 FileManager::~FileManager()
60 FileManager::setRoot(TCHAR
*drive
)
62 return _file
->setRoot(drive
);
66 FileManager::open(const TCHAR
*name
, uint32_t flags
)
68 if (!_file
->open(name
, flags
))
73 if (inflateInit2(_stream
, -15) != Z_OK
)
75 _stream
->next_in
= _inbuf
;
77 _check_header(); // skip the .gz header
86 FileManager::read(void *buf
, size_t len
, off_t ofs
)
91 return _read(buf
, len
);
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
) {
103 if (_z_err
== Z_STREAM_END
) {
106 _stream
->next_out
= reinterpret_cast<uint8_t *>(buf
);
107 _stream
->avail_out
= len
;
110 while (_stream
->avail_out
!= 0) {
112 // Copy first the lookahead bytes
113 uint32_t n
= _stream
->avail_in
;
114 if (n
> _stream
->avail_out
)
115 n
= _stream
->avail_out
;
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
,
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
);
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
;
155 /* Check for concatenated .gz files: */
157 if (_z_err
== Z_OK
) {
158 inflateReset(_stream
);
159 _crc
= crc32(0L, Z_NULL
, 0);
163 if (_z_err
!= Z_OK
|| _z_eof
)
167 _crc
= crc32(_crc
, start
,(unsigned int)(_stream
->next_out
- start
));
169 return(int)(len
- _stream
->avail_out
);
173 FileManager::write(const void *buf
, size_t bytes
, off_t ofs
)
175 return _file
->write(buf
, bytes
, ofs
);
181 return _file
->size();
189 return _file
->close();
193 FileManager::_skip_compressed(off_t toskip
)
195 #define DUMMYBUFSIZE 256
196 char dummybuf
[DUMMYBUFSIZE
];
201 size_t toread
= toskip
;
202 if (toread
> DUMMYBUFSIZE
)
203 toread
= DUMMYBUFSIZE
;
205 size_t nread
= _read(dummybuf
, toread
);
220 FileManager::realsize()
225 off_t pos
= _stream
->total_out
;
226 size_t sz
= _skip_compressed(INT_MAX
);
233 FileManager::seek(off_t offset
)
238 _stream
->avail_in
= 0;
242 /* if seek backwards, simply start from the beginning */
243 if (offset
< _stream
->total_out
) {
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
)
270 FileManager::_get_byte()
276 if (_stream
->avail_in
== 0) {
279 got
= _file
->read(_inbuf
, Z_BUFSIZE
);
284 _stream
->avail_in
= got
;
285 _stream
->next_in
= _inbuf
;
288 return *(_stream
->next_in
)++;
292 FileManager::_get_long()
294 uint32_t x
= static_cast<uint32_t>(_get_byte());
297 x
+=(static_cast<uint32_t>(_get_byte())) << 8;
298 x
+=(static_cast<uint32_t>(_get_byte())) << 16;
301 _z_err
= Z_DATA_ERROR
;
302 x
+=(static_cast<uint32_t>(c
)) << 24;
308 FileManager::_check_header()
310 int method
; /* method byte */
311 int flags
; /* flags byte */
315 /* Check the gzip magic header */
316 for (len
= 0; len
< 2; len
++) {
318 if (c
== _gz_magic
[len
])
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.
335 _z_err
= _stream
->avail_in
!= 0 ? Z_OK
: Z_STREAM_END
;
339 method
= _get_byte();
341 if (method
!= Z_DEFLATED
||(flags
& RESERVED
) != 0) {
342 _z_err
= Z_DATA_ERROR
;
346 /* Discard time, xflags and OS code: */
347 for (len
= 0; len
< 6; len
++)
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
++)
369 _z_err
= _z_eof
? Z_DATA_ERROR
: Z_OK
;