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
|
/* 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/bz2inputstream.h>
#include <bzlib.h>
#include <stdlib.h>
#include <cstring>
using namespace Strigi;
class BZ2InputStream::Private {
public:
BZ2InputStream* const p;
InputStream *input;
bz_stream bzstream;
Private(BZ2InputStream* p, InputStream* i);
~Private();
void dealloc();
void readFromStream();
bool checkMagic();
};
bool
BZ2InputStream::checkHeader(const char* data, int32_t datasize) {
static const char magic[] = {0x42, 0x5a};
static const char compressed_magic[] = {0x31, 0x41, 0x59, 0x26, 0x53, 0x59};
if (datasize < 10) return false;
return (std::memcmp(data, magic, 2) == 0
&& (data[2] == 0x68 || data[2] == 0x30)
&& std::memcmp(data + 4, compressed_magic, 6) == 0);
}
BZ2InputStream::BZ2InputStream(InputStream* input) :p(new Private(this, input)){
}
BZ2InputStream::Private::Private(BZ2InputStream* bis, InputStream* i)
:p(bis), input(i) {
// initialize values that signal state
this->input = input;
// check first bytes of stream before allocating buffer
if (!checkMagic()) {
p->m_error = "Magic bytes for bz2 are wrong.";
p->m_status = Error;
input = NULL;
return;
}
bzstream.bzalloc = NULL;
bzstream.bzfree = NULL;
bzstream.opaque = NULL;
bzstream.avail_in = 0;
bzstream.next_in = NULL;
int r;
r = BZ2_bzDecompressInit(&bzstream, 1, 0);
if (r != BZ_OK) {
p->m_error = "Error initializing BZ2InputStream.";
fprintf(stderr, "Error initializing BZ2InputStream.\n");
dealloc();
p->m_status = Error;
return;
}
// signal that we need to read into the buffer
bzstream.avail_out = 1;
// set the minimum size for the output buffer
p->setMinBufSize(262144);
}
BZ2InputStream::~BZ2InputStream() {
delete p;
}
BZ2InputStream::Private::~Private() {
dealloc();
}
void
BZ2InputStream::Private::dealloc() {
BZ2_bzDecompressEnd(&bzstream);
input = NULL;
}
bool
BZ2InputStream::Private::checkMagic() {
const char* begin;
int32_t nread;
int64_t pos = input->position();
nread = input->read(begin, 10, 0);
input->reset(pos);
if (nread < 10) {
return false;
}
return checkHeader(begin, 10);
}
void
BZ2InputStream::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 = "unexpected end of stream";
} else {
bzstream.next_in = (char*)inStart;
bzstream.avail_in = nread;
}
}
int32_t
BZ2InputStream::fillBuffer(char* start, int32_t space) {
if (p->input == NULL) return -1;
bz_stream& bzstream = p->bzstream;
// make sure there is data to decompress
if (bzstream.avail_out != 0) {
p->readFromStream();
if (m_status != Ok) {
// no data was read
return -1;
}
}
// make sure we can write into the buffer
bzstream.avail_out = space;
bzstream.next_out = start;
// decompress
int r = BZ2_bzDecompress(&bzstream);
// inform the buffer of the number of bytes that was read
int32_t nwritten = space - bzstream.avail_out;
switch (r) {
case BZ_PARAM_ERROR:
m_error = "BZ_PARAM_ERROR";
m_status = Error;
return -1;
case BZ_DATA_ERROR:
m_error = "BZ_DATA_ERROR";
m_status = Error;
return -1;
case BZ_DATA_ERROR_MAGIC:
m_error = "BZ_DATA_ERROR_MAGIC";
m_status = Error;
return -1;
case BZ_MEM_ERROR:
m_error = "BZ_MEM_ERROR";
m_status = Error;
return -1;
case BZ_STREAM_END:
if (bzstream.avail_in) {
p->input->reset(p->input->position()-bzstream.avail_in);
}
// we are finished decompressing,
// (but this stream is not yet finished)
p->dealloc();
}
return nwritten;
}
|