1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/* This file is part of Strigi Desktop Search
*
* Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <strigi/gzipinputstream.h>
#include <strigi/strigiconfig.h>
#include <zlib.h>
#include <stdlib.h>
using namespace Strigi;
class GZipInputStream::Private {
public:
GZipInputStream* const p;
InputStream* input;
z_stream_s zstream;
bool started;
Private(GZipInputStream* gi, InputStream* input, ZipFormat format);
~Private();
void dealloc();
void readFromStream();
void decompressFromStream();
bool checkMagic();
};
GZipInputStream::GZipInputStream(InputStream* input, ZipFormat format)
:p(new Private(this, input, format)) {
}
bool
GZipInputStream::checkHeader(const char* data, int32_t datasize) {
return datasize>2 && (unsigned char)data[0]==0x1f && (unsigned char)data[1]==0x8b;
}
GZipInputStream::Private::Private(GZipInputStream* gi,
InputStream* i, ZipFormat format) :p(gi), input(i), started(false) {
// initialize values that signal state
p->m_status = Ok;
// check first bytes of stream before allocating buffer
if (format == GZIPFORMAT && !checkMagic()) {
p->m_error = "Magic bytes for gz are wrong.";
p->m_status = Error;
return;
}
// initialize the z_stream
zstream.zalloc = Z_NULL;
zstream.zfree = Z_NULL;
zstream.opaque = Z_NULL;
zstream.avail_in = 0;
zstream.next_in = Z_NULL;
// initialize for reading gzip streams
// for reading libz streams, you need inflateInit(zstream)
int r;
switch(format) {
case ZLIBFORMAT:
r = inflateInit(&zstream);
break;
case GZIPFORMAT:
r = inflateInit2(&zstream, 15+16);
break;
case ZIPFORMAT:
default:
r = inflateInit2(&zstream, -MAX_WBITS);
break;
}
started = true;
if (r != Z_OK) {
p->m_error = "Error initializing GZipInputStream.";
dealloc();
p->m_status = Error;
return;
}
// signal that we need to read into the buffer
zstream.avail_out = 1;
// initialize the buffer
p->setMinBufSize(262144);
}
GZipInputStream::~GZipInputStream() {
delete p;
}
GZipInputStream::Private::~Private() {
dealloc();
}
void
GZipInputStream::Private::dealloc() {
if (started) {
inflateEnd(&zstream);
started = false;
}
memset( &zstream, 0, sizeof( z_stream_s ) );
input = NULL;
}
bool
GZipInputStream::Private::checkMagic() {
const unsigned char* buf;
const char* begin;
int32_t nread;
int64_t pos = input->position();
nread = input->read(begin, 2, 2);
input->reset(pos);
if (nread != 2) {
return false;
}
buf = (const unsigned char*)begin;
return buf[0] == 0x1f && buf[1] == 0x8b;
}
void
GZipInputStream::Private::readFromStream() {
// read data from the input stream
const char* inStart;
int32_t nread;
nread = input->read(inStart, 1, 0);
if (nread < -1) {
p->m_status = Error;
p->m_error = input->error();
} else if (nread < 1) {
p->m_status = Error;
p->m_error.assign("unexpected end of stream");
} else {
zstream.next_in = (Bytef*)inStart;
zstream.avail_in = nread;
}
}
int32_t
GZipInputStream::fillBuffer(char* start, int32_t space) {
if (p->input == NULL) return -1;
z_stream_s& zstream = p->zstream;
// make sure there is data to decompress
if (zstream.avail_out) {
p->readFromStream();
if (m_status == Error) {
// no data was read
return -1;
}
}
// make sure we can write into the buffer
zstream.avail_out = space;
zstream.next_out = (Bytef*)start;
// decompress
int r = inflate(&zstream, Z_SYNC_FLUSH);
// inform the buffer of the number of bytes that was read
int32_t nwritten = space - zstream.avail_out;
switch (r) {
case Z_NEED_DICT:
m_error.assign("Z_NEED_DICT while inflating stream.");
m_status = Error;
break;
case Z_DATA_ERROR:
m_error.assign("Z_DATA_ERROR while inflating stream.");
m_status = Error;
break;
case Z_MEM_ERROR:
m_error.assign("Z_MEM_ERROR while inflating stream.");
m_status = Error;
break;
case Z_STREAM_END:
if (zstream.avail_in) {
p->input->reset(p->input->position()-zstream.avail_in);
}
// we are finished decompressing,
// (but this stream is not yet finished)
p->dealloc();
}
return nwritten;
}
|