mirror of
https://github.com/kiwix/kiwix-tools.git
synced 2025-09-22 11:22:38 -04:00
+ remove zlib (dependence) int libzim
This commit is contained in:
parent
156a482d3b
commit
9883032447
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Tommi Maekitalo
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ZIM_DEFLATESTREAM_H
|
||||
#define ZIM_DEFLATESTREAM_H
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <zlib.h>
|
||||
#include <vector>
|
||||
|
||||
namespace zim
|
||||
{
|
||||
class DeflateError : public std::runtime_error
|
||||
{
|
||||
int zRet;
|
||||
|
||||
public:
|
||||
DeflateError(int zRet_, const std::string& msg)
|
||||
: std::runtime_error(msg),
|
||||
zRet(zRet_)
|
||||
{ }
|
||||
|
||||
int getRet() const { return zRet; }
|
||||
};
|
||||
|
||||
class DeflateStreamBuf : public std::streambuf
|
||||
{
|
||||
z_stream stream;
|
||||
std::vector<char_type> obuffer;
|
||||
std::streambuf* sink;
|
||||
|
||||
public:
|
||||
explicit DeflateStreamBuf(std::streambuf* sink_, int level = Z_DEFAULT_COMPRESSION,
|
||||
unsigned bufsize = 8192);
|
||||
~DeflateStreamBuf();
|
||||
|
||||
/// see std::streambuf
|
||||
int_type overflow(int_type c);
|
||||
/// see std::streambuf
|
||||
int_type underflow();
|
||||
/// see std::streambuf
|
||||
int sync();
|
||||
|
||||
/// end deflate-stream
|
||||
int end();
|
||||
void setSink(std::streambuf* sink_) { sink = sink_; }
|
||||
uLong getAdler() const { return stream.adler; }
|
||||
};
|
||||
|
||||
class DeflateStream : public std::ostream
|
||||
{
|
||||
DeflateStreamBuf streambuf;
|
||||
|
||||
public:
|
||||
explicit DeflateStream(std::streambuf* sink, int level = Z_DEFAULT_COMPRESSION)
|
||||
: std::ostream(0),
|
||||
streambuf(sink, level)
|
||||
{ init(&streambuf); }
|
||||
explicit DeflateStream(std::ostream& sink, int level = Z_DEFAULT_COMPRESSION)
|
||||
: std::ostream(0),
|
||||
streambuf(sink.rdbuf(), level)
|
||||
{ init(&streambuf); }
|
||||
|
||||
void end();
|
||||
void setSink(std::streambuf* sink) { streambuf.setSink(sink); }
|
||||
void setSink(std::ostream& sink) { streambuf.setSink(sink.rdbuf()); }
|
||||
uLong getAdler() const { return streambuf.getAdler(); }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ZIM_DEFLATESTREAM_H
|
||||
|
@ -1,93 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 Tommi Maekitalo
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ZIM_INFLATESTREAM_H
|
||||
#define ZIM_INFLATESTREAM_H
|
||||
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <zlib.h>
|
||||
|
||||
namespace zim
|
||||
{
|
||||
class InflateError : public std::runtime_error
|
||||
{
|
||||
int zRet;
|
||||
|
||||
public:
|
||||
InflateError(int zRet_, const std::string& msg)
|
||||
: std::runtime_error(msg),
|
||||
zRet(zRet_)
|
||||
{ }
|
||||
|
||||
int getRet() const { return zRet; }
|
||||
};
|
||||
|
||||
class InflateStreamBuf : public std::streambuf
|
||||
{
|
||||
z_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 InflateStreamBuf(std::streambuf* sinksource_, unsigned bufsize = 8192);
|
||||
~InflateStreamBuf();
|
||||
|
||||
/// 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_; }
|
||||
uLong getAdler() const { return stream.adler; }
|
||||
};
|
||||
|
||||
class InflateStream : public std::iostream
|
||||
{
|
||||
InflateStreamBuf streambuf;
|
||||
|
||||
public:
|
||||
explicit InflateStream(std::streambuf* sinksource, unsigned bufsize = 8192)
|
||||
: std::iostream(0),
|
||||
streambuf(sinksource, bufsize)
|
||||
{ init(&streambuf); }
|
||||
explicit InflateStream(std::ios& sinksource, unsigned bufsize = 8192)
|
||||
: std::iostream(0),
|
||||
streambuf(sinksource.rdbuf(), 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()); }
|
||||
uLong getAdler() const { return streambuf.getAdler(); }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // ZIM_INFLATESTREAM_H
|
||||
|
@ -9,18 +9,33 @@
|
||||
/* defined if lzma compression is enabled */
|
||||
#undef ENABLE_LZMA
|
||||
|
||||
/* defined if zlib compression is enabled */
|
||||
#undef ENABLE_ZLIB
|
||||
|
||||
/* Define to 1 if you have the <dlfcn.h> header file. */
|
||||
#undef HAVE_DLFCN_H
|
||||
|
||||
/* Define to 1 if you have the <fcntl.h> header file. */
|
||||
#undef HAVE_FCNTL_H
|
||||
|
||||
/* Define to 1 if you have the <float.h> header file. */
|
||||
#undef HAVE_FLOAT_H
|
||||
|
||||
/* Define to 1 if you have the `fork' function. */
|
||||
#undef HAVE_FORK
|
||||
|
||||
/* Define to 1 if you have the `getcwd' function. */
|
||||
#undef HAVE_GETCWD
|
||||
|
||||
/* Define to 1 if you have the `gettimeofday' function. */
|
||||
#undef HAVE_GETTIMEOFDAY
|
||||
|
||||
/* Define to 1 if you have the <inttypes.h> header file. */
|
||||
#undef HAVE_INTTYPES_H
|
||||
|
||||
/* Define to 1 if you have the `clucene' library (-lclucene). */
|
||||
#undef HAVE_LIBCLUCENE
|
||||
|
||||
/* Define to 1 if you have the <libintl.h> header file. */
|
||||
#undef HAVE_LIBINTL_H
|
||||
|
||||
/* Define to 1 if you have the `lzma' library (-llzma). */
|
||||
#undef HAVE_LIBLZMA
|
||||
|
||||
@ -30,33 +45,103 @@
|
||||
/* Define to 1 if you have the `z' library (-lz). */
|
||||
#undef HAVE_LIBZ
|
||||
|
||||
/* Define to 1 if you have the <limits.h> header file. */
|
||||
#undef HAVE_LIMITS_H
|
||||
|
||||
/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
|
||||
to 0 otherwise. */
|
||||
#undef HAVE_MALLOC
|
||||
|
||||
/* Define to 1 if you have the `memmove' function. */
|
||||
#undef HAVE_MEMMOVE
|
||||
|
||||
/* Define to 1 if you have the <memory.h> header file. */
|
||||
#undef HAVE_MEMORY_H
|
||||
|
||||
/* Define to 1 if you have the `memset' function. */
|
||||
#undef HAVE_MEMSET
|
||||
|
||||
/* Define to 1 if you have the `pow' function. */
|
||||
#undef HAVE_POW
|
||||
|
||||
/* Define to 1 if the system has the type `ptrdiff_t'. */
|
||||
#undef HAVE_PTRDIFF_T
|
||||
|
||||
/* Define to 1 if you have the `regcomp' function. */
|
||||
#undef HAVE_REGCOMP
|
||||
|
||||
/* Define to 1 if you have the `sqrt' function. */
|
||||
#undef HAVE_SQRT
|
||||
|
||||
/* Define to 1 if you have the `stat64' function. */
|
||||
#undef HAVE_STAT64
|
||||
|
||||
/* Define to 1 if stdbool.h conforms to C99. */
|
||||
#undef HAVE_STDBOOL_H
|
||||
|
||||
/* Define to 1 if you have the <stddef.h> header file. */
|
||||
#undef HAVE_STDDEF_H
|
||||
|
||||
/* Define to 1 if you have the <stdint.h> header file. */
|
||||
#undef HAVE_STDINT_H
|
||||
|
||||
/* Define to 1 if you have the <stdlib.h> header file. */
|
||||
#undef HAVE_STDLIB_H
|
||||
|
||||
/* Define to 1 if you have the `strcasecmp' function. */
|
||||
#undef HAVE_STRCASECMP
|
||||
|
||||
/* Define to 1 if you have the `strchr' function. */
|
||||
#undef HAVE_STRCHR
|
||||
|
||||
/* Define to 1 if you have the `strdup' function. */
|
||||
#undef HAVE_STRDUP
|
||||
|
||||
/* Define to 1 if you have the `strerror' function. */
|
||||
#undef HAVE_STRERROR
|
||||
|
||||
/* Define to 1 if you have the <strings.h> header file. */
|
||||
#undef HAVE_STRINGS_H
|
||||
|
||||
/* Define to 1 if you have the <string.h> header file. */
|
||||
#undef HAVE_STRING_H
|
||||
|
||||
/* Define to 1 if you have the `strtol' function. */
|
||||
#undef HAVE_STRTOL
|
||||
|
||||
/* Define to 1 if you have the <sys/socket.h> header file. */
|
||||
#undef HAVE_SYS_SOCKET_H
|
||||
|
||||
/* Define to 1 if you have the <sys/stat.h> header file. */
|
||||
#undef HAVE_SYS_STAT_H
|
||||
|
||||
/* Define to 1 if you have the <sys/time.h> header file. */
|
||||
#undef HAVE_SYS_TIME_H
|
||||
|
||||
/* Define to 1 if you have the <sys/types.h> header file. */
|
||||
#undef HAVE_SYS_TYPES_H
|
||||
|
||||
/* Define to 1 if you have the <unistd.h> header file. */
|
||||
#undef HAVE_UNISTD_H
|
||||
|
||||
/* Define to 1 if you have the `vfork' function. */
|
||||
#undef HAVE_VFORK
|
||||
|
||||
/* Define to 1 if you have the <vfork.h> header file. */
|
||||
#undef HAVE_VFORK_H
|
||||
|
||||
/* Define to 1 if you have the <wchar.h> header file. */
|
||||
#undef HAVE_WCHAR_H
|
||||
|
||||
/* Define to 1 if `fork' works. */
|
||||
#undef HAVE_WORKING_FORK
|
||||
|
||||
/* Define to 1 if `vfork' works. */
|
||||
#undef HAVE_WORKING_VFORK
|
||||
|
||||
/* Define to 1 if the system has the type `_Bool'. */
|
||||
#undef HAVE__BOOL
|
||||
|
||||
/* Define to the sub-directory in which libtool stores uninstalled libraries.
|
||||
*/
|
||||
#undef LT_OBJDIR
|
||||
@ -90,3 +175,71 @@
|
||||
|
||||
/* Version number of package */
|
||||
#undef VERSION
|
||||
|
||||
/* Define for Solaris 2.5.1 so the uint32_t typedef from <sys/synch.h>,
|
||||
<pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
|
||||
#define below would cause a syntax error. */
|
||||
#undef _UINT32_T
|
||||
|
||||
/* Define for Solaris 2.5.1 so the uint64_t typedef from <sys/synch.h>,
|
||||
<pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
|
||||
#define below would cause a syntax error. */
|
||||
#undef _UINT64_T
|
||||
|
||||
/* Define for Solaris 2.5.1 so the uint8_t typedef from <sys/synch.h>,
|
||||
<pthread.h>, or <semaphore.h> is not used. If the typedef were allowed, the
|
||||
#define below would cause a syntax error. */
|
||||
#undef _UINT8_T
|
||||
|
||||
/* Define to `__inline__' or `__inline' if that's what the C compiler
|
||||
calls it, or to nothing if 'inline' is not supported under any name. */
|
||||
#ifndef __cplusplus
|
||||
#undef inline
|
||||
#endif
|
||||
|
||||
/* Define to the type of a signed integer type of width exactly 16 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef int16_t
|
||||
|
||||
/* Define to the type of a signed integer type of width exactly 32 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef int32_t
|
||||
|
||||
/* Define to the type of a signed integer type of width exactly 64 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef int64_t
|
||||
|
||||
/* Define to the type of a signed integer type of width exactly 8 bits if such
|
||||
a type exists and the standard includes do not define it. */
|
||||
#undef int8_t
|
||||
|
||||
/* Define to rpl_malloc if the replacement function should be used. */
|
||||
#undef malloc
|
||||
|
||||
/* Define to `long int' if <sys/types.h> does not define. */
|
||||
#undef off_t
|
||||
|
||||
/* Define to `int' if <sys/types.h> does not define. */
|
||||
#undef pid_t
|
||||
|
||||
/* Define to `unsigned int' if <sys/types.h> does not define. */
|
||||
#undef size_t
|
||||
|
||||
/* Define to the type of an unsigned integer type of width exactly 16 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef uint16_t
|
||||
|
||||
/* Define to the type of an unsigned integer type of width exactly 32 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef uint32_t
|
||||
|
||||
/* Define to the type of an unsigned integer type of width exactly 64 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef uint64_t
|
||||
|
||||
/* Define to the type of an unsigned integer type of width exactly 8 bits if
|
||||
such a type exists and the standard includes do not define it. */
|
||||
#undef uint8_t
|
||||
|
||||
/* Define as `fork' if `vfork' does not work. */
|
||||
#undef vfork
|
||||
|
@ -1,176 +0,0 @@
|
||||
/* deflatestream.cpp
|
||||
* Copyright (C) 2003-2005 Tommi Maekitalo
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "zim/deflatestream.h"
|
||||
#include "log.h"
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
|
||||
log_define("zim.deflatestream")
|
||||
|
||||
namespace zim
|
||||
{
|
||||
namespace
|
||||
{
|
||||
int checkError(int ret, z_stream& stream)
|
||||
{
|
||||
if (ret != Z_OK && ret != Z_STREAM_END)
|
||||
{
|
||||
log_error("DeflateError " << ret << ": \"" << (stream.msg ? stream.msg : "") << '"');
|
||||
std::ostringstream msg;
|
||||
msg << "deflate-error " << ret;
|
||||
if (stream.msg)
|
||||
msg << ": " << stream.msg;
|
||||
throw DeflateError(ret, msg.str());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
DeflateStreamBuf::DeflateStreamBuf(std::streambuf* sink_, int level, unsigned bufsize_)
|
||||
: obuffer(bufsize_),
|
||||
sink(sink_)
|
||||
{
|
||||
memset(&stream, 0, sizeof(z_stream));
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = 0;
|
||||
stream.total_out = 0;
|
||||
stream.total_in = 0;
|
||||
stream.next_in = Z_NULL;
|
||||
stream.next_out = Z_NULL;
|
||||
stream.avail_in = 0;
|
||||
stream.avail_out = 0;
|
||||
|
||||
checkError(::deflateInit(&stream, level), stream);
|
||||
setp(&obuffer[0], &obuffer[0] + obuffer.size());
|
||||
}
|
||||
|
||||
DeflateStreamBuf::~DeflateStreamBuf()
|
||||
{
|
||||
::deflateEnd(&stream);
|
||||
}
|
||||
|
||||
DeflateStreamBuf::int_type DeflateStreamBuf::overflow(int_type c)
|
||||
{
|
||||
// initialize input-stream
|
||||
stream.next_in = reinterpret_cast<Bytef*>(&obuffer[0]);
|
||||
stream.avail_in = pptr() - &obuffer[0];
|
||||
|
||||
// initialize zbuffer for deflated data
|
||||
char zbuffer[8192];
|
||||
stream.next_out = reinterpret_cast<Bytef*>(zbuffer);
|
||||
stream.avail_out = sizeof(zbuffer);
|
||||
|
||||
// deflate
|
||||
checkError(::deflate(&stream, Z_NO_FLUSH), 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;
|
||||
}
|
||||
|
||||
DeflateStreamBuf::int_type DeflateStreamBuf::underflow()
|
||||
{
|
||||
return traits_type::eof();
|
||||
}
|
||||
|
||||
int DeflateStreamBuf::sync()
|
||||
{
|
||||
// initialize input-stream for
|
||||
stream.next_in = reinterpret_cast<Bytef*>(&obuffer[0]);
|
||||
stream.avail_in = pptr() - &obuffer[0];
|
||||
char zbuffer[8192];
|
||||
while (stream.avail_in > 0)
|
||||
{
|
||||
// initialize zbuffer
|
||||
stream.next_out = (Bytef*)zbuffer;
|
||||
stream.avail_out = sizeof(zbuffer);
|
||||
|
||||
checkError(::deflate(&stream, Z_SYNC_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;
|
||||
}
|
||||
};
|
||||
|
||||
// reset outbuffer
|
||||
setp(&obuffer[0], &obuffer[0] + obuffer.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DeflateStreamBuf::end()
|
||||
{
|
||||
char zbuffer[8192];
|
||||
// initialize input-stream for
|
||||
stream.next_in = reinterpret_cast<Bytef*>(&obuffer[0]);
|
||||
stream.avail_in = pptr() - &obuffer[0];
|
||||
while (true)
|
||||
{
|
||||
// initialize zbuffer
|
||||
stream.next_out = (Bytef*)zbuffer;
|
||||
stream.avail_out = sizeof(zbuffer);
|
||||
|
||||
int ret = checkError(::deflate(&stream, Z_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)
|
||||
throw DeflateError(0, "failed to send compressed data to sink in deflatestream");
|
||||
}
|
||||
if (ret == Z_STREAM_END)
|
||||
break;
|
||||
};
|
||||
|
||||
// reset outbuffer
|
||||
setp(&obuffer[0], &obuffer[0] + obuffer.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
void DeflateStream::end()
|
||||
{
|
||||
if (streambuf.end() != 0)
|
||||
setstate(failbit);
|
||||
}
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
/* inflatestream.cpp
|
||||
* Copyright (C) 2003-2005 Tommi Maekitalo
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 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
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "zim/inflatestream.h"
|
||||
#include "log.h"
|
||||
#include <sstream>
|
||||
|
||||
log_define("zim.inflatestream")
|
||||
|
||||
namespace zim
|
||||
{
|
||||
namespace
|
||||
{
|
||||
int checkError(int ret, z_stream& stream)
|
||||
{
|
||||
if (ret != Z_OK && ret != Z_STREAM_END)
|
||||
{
|
||||
log_error("InflateError " << ret << ": \"" << (stream.msg ? stream.msg : "") << '"');
|
||||
std::ostringstream msg;
|
||||
msg << "inflate-error " << ret;
|
||||
if (stream.msg)
|
||||
msg << ": " << stream.msg;
|
||||
throw InflateError(ret, msg.str());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
InflateStreamBuf::InflateStreamBuf(std::streambuf* sinksource_, unsigned bufsize_)
|
||||
: iobuffer(new char_type[bufsize_]),
|
||||
bufsize(bufsize_),
|
||||
sinksource(sinksource_)
|
||||
{
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = 0;
|
||||
stream.total_out = 0;
|
||||
stream.total_in = 0;
|
||||
stream.next_in = Z_NULL;
|
||||
stream.avail_in = 0;
|
||||
|
||||
checkError(::inflateInit(&stream), stream);
|
||||
}
|
||||
|
||||
InflateStreamBuf::~InflateStreamBuf()
|
||||
{
|
||||
::inflateEnd(&stream);
|
||||
delete[] iobuffer;
|
||||
}
|
||||
|
||||
InflateStreamBuf::int_type InflateStreamBuf::overflow(int_type c)
|
||||
{
|
||||
if (pptr())
|
||||
{
|
||||
// initialize input-stream for
|
||||
stream.next_in = (Bytef*)obuffer();
|
||||
stream.avail_in = pptr() - pbase();
|
||||
|
||||
int ret;
|
||||
do
|
||||
{
|
||||
// initialize ibuffer
|
||||
stream.next_out = (Bytef*)ibuffer();
|
||||
stream.avail_out = ibuffer_size();
|
||||
|
||||
ret = ::inflate(&stream, Z_SYNC_FLUSH);
|
||||
checkError(ret, stream);
|
||||
|
||||
// copy zbuffer 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 != Z_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;
|
||||
}
|
||||
|
||||
InflateStreamBuf::int_type InflateStreamBuf::underflow()
|
||||
{
|
||||
// read from sinksource and decompress into obuffer
|
||||
|
||||
stream.next_out = (Bytef*)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(), std::min(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 = (Bytef*)ibuffer();
|
||||
}
|
||||
|
||||
// we decompress it now into obuffer
|
||||
|
||||
// at least one character received from source - pass to decompressor
|
||||
|
||||
int ret = ::inflate(&stream, Z_SYNC_FLUSH);
|
||||
|
||||
checkError(ret, stream);
|
||||
|
||||
setg(obuffer(), obuffer(), obuffer() + obuffer_size() - stream.avail_out);
|
||||
|
||||
} while (gptr() == egptr());
|
||||
|
||||
return sgetc();
|
||||
}
|
||||
|
||||
int InflateStreamBuf::sync()
|
||||
{
|
||||
if (pptr() && overflow(traits_type::eof()) == traits_type::eof())
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user