+ remove bzip2 (deprecated compression algorithm) relatif code in libzim

This commit is contained in:
kelson42 2010-11-21 15:20:35 +00:00
parent 5f97db25e0
commit 8ec37af0ab
7 changed files with 0 additions and 587 deletions

View File

@ -1,87 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ZIM_BUNZIP2STREAM_H
#define ZIM_BUNZIP2STREAM_H
#include <iostream>
#include <stdexcept>
#include <bzlib.h>
#include <zim/bzip2.h>
namespace zim
{
class Bzip2UncompressError : public Bzip2Error
{
public:
Bzip2UncompressError(int zRet_, const std::string& msg)
: Bzip2Error(zRet_, msg)
{ }
};
class Bunzip2StreamBuf : public std::streambuf
{
bz_stream stream;
char_type* iobuffer;
unsigned bufsize;
std::streambuf* sinksource;
char_type* ibuffer() { return iobuffer; }
std::streamsize ibuffer_size() { return bufsize >> 1; }
char_type* obuffer() { return iobuffer + ibuffer_size(); }
std::streamsize obuffer_size() { return bufsize >> 1; }
public:
explicit Bunzip2StreamBuf(std::streambuf* sinksource_, bool small = false, unsigned bufsize = 8192);
~Bunzip2StreamBuf();
/// see std::streambuf
int_type overflow(int_type c);
/// see std::streambuf
int_type underflow();
/// see std::streambuf
int sync();
void setSinksource(std::streambuf* sinksource_) { sinksource = sinksource_; }
};
class Bunzip2Stream : public std::iostream
{
Bunzip2StreamBuf streambuf;
public:
explicit Bunzip2Stream(std::streambuf* sinksource, bool small = false, unsigned bufsize = 8192)
: std::iostream(0),
streambuf(sinksource, small, bufsize)
{ init(&streambuf); }
explicit Bunzip2Stream(std::ios& sinksource, bool small = false, unsigned bufsize = 8192)
: std::iostream(0),
streambuf(sinksource.rdbuf(), small, bufsize)
{ init(&streambuf); }
void setSinksource(std::streambuf* sinksource) { streambuf.setSinksource(sinksource); }
void setSinksource(std::ios& sinksource) { streambuf.setSinksource(sinksource.rdbuf()); }
void setSink(std::ostream& sink) { streambuf.setSinksource(sink.rdbuf()); }
void setSource(std::istream& source) { streambuf.setSinksource(source.rdbuf()); }
};
}
#endif // ZIM_BUNZIP2STREAM_H

View File

@ -1,44 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ZIM_BZIP2_H
#define ZIM_BZIP2_H
#include <stdexcept>
namespace zim
{
class Bzip2Error : public std::runtime_error
{
int zRet;
public:
Bzip2Error(int zRet_, const std::string& msg)
: std::runtime_error(msg),
zRet(zRet_)
{ }
int getRet() const { return zRet; }
static const char* getErrorString(int ret);
};
}
#endif // ZIM_BZIP2_H

View File

@ -1,85 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef ZIM_BZIP2STREAM_H
#define ZIM_BZIP2STREAM_H
#include <iostream>
#include <stdexcept>
#include <bzlib.h>
#include <vector>
#include <zim/bzip2.h>
namespace zim
{
class Bzip2CompressError : public Bzip2Error
{
public:
Bzip2CompressError(int zRet_, const std::string& msg)
: Bzip2Error(zRet_, msg)
{ }
};
class Bzip2StreamBuf : public std::streambuf
{
bz_stream stream;
std::vector<char_type> obuffer;
std::streambuf* sink;
public:
explicit Bzip2StreamBuf(std::streambuf* sink_, int blockSize100k = 9,
int workFactor = 30, unsigned bufsize = 8192);
~Bzip2StreamBuf();
/// see std::streambuf
int_type overflow(int_type c);
/// see std::streambuf
int_type underflow();
/// see std::streambuf
int sync();
/// end bzip2-stream
int end();
void setSink(std::streambuf* sink_) { sink = sink_; }
};
class Bzip2Stream : public std::ostream
{
Bzip2StreamBuf streambuf;
public:
explicit Bzip2Stream(std::streambuf* sink, int blockSize100k = 9,
int workFactor = 30, unsigned bufsize = 8192)
: std::ostream(0),
streambuf(sink, blockSize100k, workFactor, bufsize)
{ init(&streambuf); }
explicit Bzip2Stream(std::ostream& sink, int blockSize100k = 9,
int workFactor = 30, unsigned bufsize = 8192)
: std::ostream(0),
streambuf(sink.rdbuf(), blockSize100k, workFactor, bufsize)
{ init(&streambuf); }
void end();
void setSink(std::streambuf* sink) { streambuf.setSink(sink); }
void setSink(std::ostream& sink) { streambuf.setSink(sink.rdbuf()); }
};
}
#endif // ZIM_BZIP2STREAM_H

View File

@ -1,151 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <zim/bunzip2stream.h>
#include "log.h"
#include <sstream>
#include <cstring>
#include <algorithm>
log_define("zim.bzip2.uncompress")
namespace zim
{
namespace
{
int checkError(int ret, bz_stream& stream)
{
if (ret != BZ_OK && ret != BZ_RUN_OK && ret != BZ_FLUSH_OK && ret != BZ_FINISH_OK
&& ret != BZ_STREAM_END)
{
std::ostringstream msg;
msg << "bzip2-error " << ret << ": " << Bzip2Error::getErrorString(ret);
log_error(msg.str());
throw Bzip2UncompressError(ret, msg.str());
}
return ret;
}
// because in windows std::min is not defined, we define our own min-function
template <typename T>
bool mymin(const T& a, const T& b)
{ return a <= b ? a : b; }
}
Bunzip2StreamBuf::Bunzip2StreamBuf(std::streambuf* sinksource_, bool small, unsigned bufsize_)
: iobuffer(new char_type[bufsize_]),
bufsize(bufsize_),
sinksource(sinksource_)
{
std::memset(&stream, 0, sizeof(bz_stream));
checkError(::BZ2_bzDecompressInit(&stream, 0, static_cast<int>(small)), stream);
}
Bunzip2StreamBuf::~Bunzip2StreamBuf()
{
::BZ2_bzDecompressEnd(&stream);
delete[] iobuffer;
}
Bunzip2StreamBuf::int_type Bunzip2StreamBuf::overflow(int_type c)
{
if (pptr())
{
// initialize input-stream for
stream.next_in = obuffer();
stream.avail_in = pptr() - pbase();
int ret;
do
{
// initialize ibuffer
stream.next_out = ibuffer();
stream.avail_out = ibuffer_size();
ret = ::BZ2_bzDecompress(&stream);
checkError(ret, stream);
// copy ibuffer to sinksource
std::streamsize count = ibuffer_size() - stream.avail_out;
std::streamsize n = sinksource->sputn(reinterpret_cast<char*>(ibuffer()), count);
if (n < count)
return traits_type::eof();
} while (ret != BZ_STREAM_END && stream.avail_in > 0);
}
// reset outbuffer
setp(obuffer(), obuffer() + obuffer_size());
if (c != traits_type::eof())
sputc(traits_type::to_char_type(c));
return 0;
}
Bunzip2StreamBuf::int_type Bunzip2StreamBuf::underflow()
{
// read from sinksource and decompress into obuffer
stream.next_out = obuffer();
stream.avail_out = obuffer_size();
do
{
// fill ibuffer first if needed
if (stream.avail_in == 0)
{
if (sinksource->in_avail() > 0)
{
// there is data already available
// read compressed data from source into ibuffer
stream.avail_in = sinksource->sgetn(ibuffer(), mymin(sinksource->in_avail(), ibuffer_size()));
}
else
{
// no data available
stream.avail_in = sinksource->sgetn(ibuffer(), ibuffer_size());
if (stream.avail_in == 0)
return traits_type::eof();
}
stream.next_in = ibuffer();
}
// we decompress it now into obuffer
// at least one character received from source - pass to decompressor
int ret = ::BZ2_bzDecompress(&stream);
checkError(ret, stream);
setg(obuffer(), obuffer(), obuffer() + obuffer_size() - stream.avail_out);
} while (gptr() == egptr());
return sgetc();
}
int Bunzip2StreamBuf::sync()
{
if (pptr() && overflow(traits_type::eof()) == traits_type::eof())
return -1;
return 0;
}
}

View File

@ -1,46 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <zim/bzip2.h>
#include <bzlib.h>
namespace zim
{
const char* Bzip2Error::getErrorString(int ret)
{
switch (ret)
{
case BZ_OK: return "BZ_OK";
case BZ_RUN_OK: return "BZ_RUN_OK";
case BZ_FLUSH_OK: return "BZ_FLUSH_OK";
case BZ_FINISH_OK: return "BZ_FINISH_OK";
case BZ_STREAM_END: return "BZ_STREAM_END";
case BZ_CONFIG_ERROR: return "BZ_CONFIG_ERROR";
case BZ_SEQUENCE_ERROR: return "BZ_SEQUENCE_ERROR";
case BZ_PARAM_ERROR: return "BZ_PARAM_ERROR";
case BZ_MEM_ERROR: return "BZ_MEM_ERROR";
case BZ_DATA_ERROR: return "BZ_DATA_ERROR";
case BZ_DATA_ERROR_MAGIC: return "BZ_DATA_ERROR_MAGIC";
case BZ_IO_ERROR: return "BZ_IO_ERROR";
case BZ_UNEXPECTED_EOF: return "BZ_UNEXPECTED_EOF";
case BZ_OUTBUFF_FULL: return "BZ_OUTBUFF_FULL";
default: return "unknown error";
}
}
}

View File

@ -1,168 +0,0 @@
/*
* Copyright (C) 2008 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <zim/bzip2stream.h>
#include "log.h"
#include <sstream>
#include <string.h>
log_define("zim.bzip2.compress")
namespace zim
{
namespace
{
int checkError(int ret, bz_stream& stream)
{
if (ret != BZ_OK && ret != BZ_RUN_OK && ret != BZ_FLUSH_OK && ret != BZ_FINISH_OK
&& ret != BZ_STREAM_END)
{
std::ostringstream msg;
msg << "bzip2-error " << ret << ": " << Bzip2Error::getErrorString(ret);
log_error(msg.str());
throw Bzip2CompressError(ret, msg.str());
}
return ret;
}
}
Bzip2StreamBuf::Bzip2StreamBuf(std::streambuf* sink_, int blockSize100k,
int workFactor, unsigned bufsize_)
: obuffer(bufsize_),
sink(sink_)
{
memset(&stream, 0, sizeof(bz_stream));
checkError(::BZ2_bzCompressInit(&stream, blockSize100k, 0, workFactor),
stream);
setp(&obuffer[0], &obuffer[0] + obuffer.size());
}
Bzip2StreamBuf::~Bzip2StreamBuf()
{
::BZ2_bzCompressEnd(&stream);
}
Bzip2StreamBuf::int_type Bzip2StreamBuf::overflow(int_type c)
{
// initialize input-stream
stream.next_in = &obuffer[0];
stream.avail_in = pptr() - &obuffer[0];
// initialize zbuffer for deflated data
char zbuffer[8192];
stream.next_out = zbuffer;
stream.avail_out = sizeof(zbuffer);
// deflate
checkError(::BZ2_bzCompress(&stream, BZ_RUN), stream);
// copy zbuffer to sink / consume deflated data
std::streamsize count = sizeof(zbuffer) - stream.avail_out;
if (count > 0)
{
std::streamsize n = sink->sputn(zbuffer, count);
if (n < count)
return traits_type::eof();
}
// move remaining characters to start of obuffer
if (stream.avail_in > 0)
memmove(&obuffer[0], stream.next_in, stream.avail_in);
// reset outbuffer
setp(&obuffer[0] + stream.avail_in, &obuffer[0] + obuffer.size());
if (c != traits_type::eof())
sputc(traits_type::to_char_type(c));
return 0;
}
Bzip2StreamBuf::int_type Bzip2StreamBuf::underflow()
{
return traits_type::eof();
}
int Bzip2StreamBuf::sync()
{
// initialize input-stream for
stream.next_in = &obuffer[0];
stream.avail_in = pptr() - pbase();
char zbuffer[8192];
int ret;
do
{
// initialize zbuffer
stream.next_out = zbuffer;
stream.avail_out = sizeof(zbuffer);
ret = checkError(::BZ2_bzCompress(&stream, BZ_FLUSH), stream);
// copy zbuffer to sink
std::streamsize count = sizeof(zbuffer) - stream.avail_out;
if (count > 0)
{
std::streamsize n = sink->sputn(zbuffer, count);
if (n < count)
return -1;
}
} while (ret != BZ_RUN_OK);
// reset outbuffer
setp(&obuffer[0], &obuffer[0] + obuffer.size());
return 0;
}
int Bzip2StreamBuf::end()
{
char zbuffer[8192];
// initialize input-stream for
stream.next_in = &obuffer[0];
stream.avail_in = pptr() - pbase();
int ret;
do
{
// initialize zbuffer
stream.next_out = zbuffer;
stream.avail_out = sizeof(zbuffer);
ret = checkError(::BZ2_bzCompress(&stream, BZ_FINISH), stream);
// copy zbuffer to sink
std::streamsize count = sizeof(zbuffer) - stream.avail_out;
if (count > 0)
{
std::streamsize n = sink->sputn(zbuffer, count);
if (n < count)
return -1;
}
} while (ret != BZ_STREAM_END);
// reset outbuffer
setp(&obuffer[0], &obuffer[0] + obuffer.size());
return 0;
}
void Bzip2Stream::end()
{
if (streambuf.end() != 0)
setstate(failbit);
}
}

View File

@ -6,9 +6,6 @@
/* set zim dirent cache size to number of cached chunks */
#undef DIRENT_CACHE_SIZE
/* defined if bzip2 compression is enabled */
#undef ENABLE_BZIP2
/* defined if lzma compression is enabled */
#undef ENABLE_LZMA
@ -21,9 +18,6 @@
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the `bz2' library (-lbz2). */
#undef HAVE_LIBBZ2
/* Define to 1 if you have the `clucene' library (-lclucene). */
#undef HAVE_LIBCLUCENE