forked from kiwix/kiwix-tools
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
|
|
||||||
|
|
||||||
@ -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…
Reference in new issue