#include <console.h>
#include <file.h>
#include <limits.h>
__BEGIN_DECLS
#include <string.h>
#include <zlib.h>
uLong crc32(uLong crc, const Bytef *buf, uInt len);
__END_DECLS
static struct z_stream_s __stream;
void
FileManager::_reset()
{
_stream = &__stream;
memset(_stream, 0, sizeof(struct z_stream_s));
_z_err = 0;
_z_eof = 0;
_crc = 0;
_compressed = 0;
}
FileManager::~FileManager()
{
delete _file;
}
BOOL
FileManager::setRoot(TCHAR *drive)
{
return _file->setRoot(drive);
}
BOOL
FileManager::open(const TCHAR *name, uint32_t flags)
{
if (!_file->open(name, flags))
return FALSE;
_reset();
if (inflateInit2(_stream, -15) != Z_OK)
goto errout;
_stream->next_in = _inbuf;
_check_header();
return TRUE;
errout:
_file->close();
return FALSE;
}
size_t
FileManager::read(void *buf, size_t len, off_t ofs)
{
if (ofs != -1)
seek(ofs);
return _read(buf, len);
}
size_t
FileManager::_read(void *buf, size_t len)
{
uint8_t *start = reinterpret_cast<uint8_t *>(buf);
if (_z_err == Z_DATA_ERROR || _z_err == Z_ERRNO) {
return -1;
}
if (_z_err == Z_STREAM_END) {
return 0;
}
_stream->next_out = reinterpret_cast<uint8_t *>(buf);
_stream->avail_out = len;
int got;
while (_stream->avail_out != 0) {
if (!_compressed) {
uint32_t n = _stream->avail_in;
if (n > _stream->avail_out)
n = _stream->avail_out;
if (n > 0) {
memcpy(_stream->next_out, _stream->next_in, n);
_stream->next_out += n;
_stream->next_in += n;
_stream->avail_out -= n;
_stream->avail_in -= n;
}
if (_stream->avail_out > 0) {
got = _file->read(_stream->next_out,
_stream->avail_out);
if (got == -1) {
return(got);
}
_stream->avail_out -= got;
}
return(int)(len - _stream->avail_out);
}
if (_stream->avail_in == 0 && !_z_eof) {
got = _file->read(_inbuf, Z_BUFSIZE);
if (got <= 0)
_z_eof = 1;
_stream->avail_in = got;
_stream->next_in = _inbuf;
}
_z_err = inflate(_stream, Z_NO_FLUSH);
if (_z_err == Z_STREAM_END) {
_crc = crc32(_crc, start,(unsigned int)
(_stream->next_out - start));
start = _stream->next_out;
if (_get_long() != _crc ||
_get_long() != _stream->total_out) {
_z_err = Z_DATA_ERROR;
} else {
_check_header();
if (_z_err == Z_OK) {
inflateReset(_stream);
_crc = crc32(0L, Z_NULL, 0);
}
}
}
if (_z_err != Z_OK || _z_eof)
break;
}
_crc = crc32(_crc, start,(unsigned int)(_stream->next_out - start));
return(int)(len - _stream->avail_out);
}
size_t
FileManager::write(const void *buf, size_t bytes, off_t ofs)
{
return _file->write(buf, bytes, ofs);
}
size_t
FileManager::size()
{
return _file->size();
}
BOOL
FileManager::close()
{
inflateEnd(_stream);
return _file->close();
}
size_t
FileManager::_skip_compressed(off_t toskip)
{
#define DUMMYBUFSIZE 256
char dummybuf[DUMMYBUFSIZE];
size_t skipped = 0;
while (toskip > 0) {
size_t toread = toskip;
if (toread > DUMMYBUFSIZE)
toread = DUMMYBUFSIZE;
size_t nread = _read(dummybuf, toread);
if ((int)nread < 0)
return nread;
toskip -= nread;
skipped += nread;
if (nread != toread)
break;
}
return skipped;
}
size_t
FileManager::realsize()
{
if (!_compressed)
return size();
off_t pos = _stream->total_out;
size_t sz = _skip_compressed(INT_MAX);
seek(pos);
return sz;
}
BOOL
FileManager::seek(off_t offset)
{
if (!_compressed) {
_file->seek(offset);
_stream->avail_in = 0;
return TRUE;
}
if (offset < _stream->total_out) {
_file->seek(0);
inflateEnd(_stream);
_reset();
inflateInit2(_stream, -15);
_stream->next_in = _inbuf;
_check_header();
}
if (offset > _stream->total_out) {
off_t toskip = offset - _stream->total_out;
size_t skipped = _skip_compressed(toskip);
if (skipped != toskip)
return FALSE;
}
return TRUE;
}
int
FileManager::_get_byte()
{
if (_z_eof)
return(EOF);
if (_stream->avail_in == 0) {
int got;
got = _file->read(_inbuf, Z_BUFSIZE);
if (got <= 0) {
_z_eof = 1;
return EOF;
}
_stream->avail_in = got;
_stream->next_in = _inbuf;
}
_stream->avail_in--;
return *(_stream->next_in)++;
}
uint32_t
FileManager::_get_long()
{
uint32_t x = static_cast<uint32_t>(_get_byte());
int c;
x +=(static_cast<uint32_t>(_get_byte())) << 8;
x +=(static_cast<uint32_t>(_get_byte())) << 16;
c = _get_byte();
if (c == EOF)
_z_err = Z_DATA_ERROR;
x +=(static_cast<uint32_t>(c)) << 24;
return x;
}
void
FileManager::_check_header()
{
int method;
int flags;
unsigned int len;
int c;
for (len = 0; len < 2; len++) {
c = _get_byte();
if (c == _gz_magic[len])
continue;
if ((c == EOF) &&(len == 0)) {
return;
}
_compressed = 0;
if (c != EOF) {
_stream->avail_in++;
_stream->next_in--;
}
_z_err = _stream->avail_in != 0 ? Z_OK : Z_STREAM_END;
return;
}
_compressed = 1;
method = _get_byte();
flags = _get_byte();
if (method != Z_DEFLATED ||(flags & RESERVED) != 0) {
_z_err = Z_DATA_ERROR;
return;
}
for (len = 0; len < 6; len++)
(void)_get_byte();
if ((flags & EXTRA_FIELD) != 0) {
len = (unsigned int)_get_byte();
len +=((unsigned int)_get_byte()) << 8;
while (len-- != 0 && _get_byte() != EOF) ;
}
if ((flags & ORIG_NAME) != 0) {
while ((c = _get_byte()) != 0 && c != EOF) ;
}
if ((flags & COMMENT) != 0) {
while ((c = _get_byte()) != 0 && c != EOF) ;
}
if ((flags & HEAD_CRC) != 0) {
for (len = 0; len < 2; len++)
(void)_get_byte();
}
_z_err = _z_eof ? Z_DATA_ERROR : Z_OK;
}