2009-09-22 01:05:05 +02:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2014-10-04 20:04:03 +02:00
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2014-02-12 17:30:49 +01:00
|
|
|
* Copyright (c) 2008-2014, Tomasz Sowa
|
2009-09-22 01:05:05 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2014-10-04 20:04:03 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2009-09-22 01:05:05 +02:00
|
|
|
*/
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
#include "compress.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
namespace Winix
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
Compress::Compress()
|
|
|
|
{
|
2012-09-11 07:19:45 +02:00
|
|
|
buffer_in = 0;
|
|
|
|
buffer_out = 0;
|
2011-04-02 02:10:16 +02:00
|
|
|
buffer_max_len = 65536; // 64KB
|
2009-10-08 19:59:10 +02:00
|
|
|
ready_for_compress = false;
|
2011-04-02 02:10:16 +02:00
|
|
|
compress_level = 6;
|
|
|
|
raw_deflate_inited = false;
|
|
|
|
deflate_inited = false;
|
|
|
|
gzip_inited = false;
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Compress::~Compress()
|
|
|
|
{
|
2012-09-11 07:19:45 +02:00
|
|
|
delete [] buffer_in;
|
|
|
|
delete [] buffer_out;
|
2011-04-02 02:10:16 +02:00
|
|
|
|
|
|
|
if( raw_deflate_inited )
|
|
|
|
deflateEnd(&strm_raw_deflate);
|
|
|
|
|
|
|
|
if( deflate_inited )
|
|
|
|
deflateEnd(&strm_deflate);
|
|
|
|
|
|
|
|
if( gzip_inited )
|
|
|
|
deflateEnd(&strm_gzip);
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Compress::AllocateMemory()
|
|
|
|
{
|
2012-09-11 07:19:45 +02:00
|
|
|
if( buffer_in )
|
|
|
|
delete [] buffer_in;
|
|
|
|
|
|
|
|
if( buffer_out )
|
|
|
|
delete [] buffer_out;
|
|
|
|
|
|
|
|
buffer_in = 0;
|
|
|
|
buffer_out = 0;
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2012-09-11 07:19:45 +02:00
|
|
|
buffer_in = new char[buffer_max_len];
|
|
|
|
buffer_out = new char[buffer_max_len];
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
catch(const std::bad_alloc &)
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: can't allocate memory" << logend;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-02 02:10:16 +02:00
|
|
|
|
|
|
|
bool Compress::InitRawDeflate()
|
|
|
|
{
|
|
|
|
raw_deflate_inited = false;
|
|
|
|
|
|
|
|
strm_raw_deflate.next_in = 0;
|
|
|
|
strm_raw_deflate.zalloc = Z_NULL;
|
|
|
|
strm_raw_deflate.zfree = Z_NULL;
|
|
|
|
strm_raw_deflate.opaque = Z_NULL;
|
|
|
|
|
|
|
|
int ret = deflateInit2(&strm_raw_deflate, compress_level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
|
|
|
|
if( ret != Z_OK )
|
|
|
|
log << log1 << "Compress: problem with deflateInit2() for raw deflate" << logend;
|
|
|
|
else
|
|
|
|
raw_deflate_inited = true;
|
|
|
|
|
|
|
|
return ret == Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Compress::InitDeflate()
|
|
|
|
{
|
|
|
|
deflate_inited = false;
|
|
|
|
|
|
|
|
strm_deflate.next_in = 0;
|
|
|
|
strm_deflate.zalloc = Z_NULL;
|
|
|
|
strm_deflate.zfree = Z_NULL;
|
|
|
|
strm_deflate.opaque = Z_NULL;
|
|
|
|
|
|
|
|
int ret = deflateInit2(&strm_deflate, compress_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
|
|
|
|
if( ret != Z_OK )
|
|
|
|
log << log1 << "Compress: problem with deflateInit2() for deflate" << logend;
|
|
|
|
else
|
|
|
|
deflate_inited = true;
|
|
|
|
|
|
|
|
return ret == Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Compress::InitGzip()
|
|
|
|
{
|
|
|
|
gzip_inited = false;
|
|
|
|
|
|
|
|
strm_gzip.next_in = 0;
|
|
|
|
strm_gzip.zalloc = Z_NULL;
|
|
|
|
strm_gzip.zfree = Z_NULL;
|
|
|
|
strm_gzip.opaque = Z_NULL;
|
|
|
|
|
|
|
|
int ret = deflateInit2(&strm_gzip, compress_level, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
|
|
|
|
if( ret != Z_OK )
|
|
|
|
log << log1 << "Compress: problem with deflateInit2() for gzip" << logend;
|
|
|
|
else
|
|
|
|
gzip_inited = true;
|
|
|
|
|
|
|
|
return ret == Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
/*
|
|
|
|
return:
|
2011-04-02 02:10:16 +02:00
|
|
|
0 - ok
|
|
|
|
1 - can't allocate memory
|
|
|
|
100 - unknown error
|
2009-10-08 19:59:10 +02:00
|
|
|
*/
|
2011-04-02 02:10:16 +02:00
|
|
|
int Compress::Init(int compress_level_)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
2011-04-02 02:10:16 +02:00
|
|
|
compress_level = compress_level_;
|
|
|
|
|
2012-09-11 07:19:45 +02:00
|
|
|
if( !AllocateMemory() )
|
|
|
|
return 1;
|
2009-10-08 19:59:10 +02:00
|
|
|
|
2011-04-02 02:10:16 +02:00
|
|
|
if( InitRawDeflate() && InitDeflate() && InitGzip() )
|
|
|
|
ready_for_compress = true;
|
|
|
|
else
|
|
|
|
return 100;
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-02 02:10:16 +02:00
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
int Compress::MakeCompress(z_stream & strm, const char * source, size_t source_len, BinaryPage & out_stream, int encoding)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
|
|
|
int ret, flush;
|
|
|
|
size_t have;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.avail_in = (source_len > buffer_max_len) ? buffer_max_len : source_len;
|
|
|
|
source_len -= strm.avail_in;
|
|
|
|
flush = (source_len == 0) ? Z_FINISH : Z_NO_FLUSH;
|
|
|
|
strm.next_in = (Bytef*)source;
|
|
|
|
source += strm.avail_in;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.avail_out = buffer_max_len;
|
2012-09-11 07:19:45 +02:00
|
|
|
strm.next_out = (Bytef*)buffer_out;
|
|
|
|
ret = deflate(&strm, flush);
|
|
|
|
|
|
|
|
if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate()" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
have = buffer_max_len - strm.avail_out;
|
|
|
|
last_out_size += have;
|
2013-12-04 02:21:57 +01:00
|
|
|
out_stream.write(buffer_out, have);
|
2012-09-11 07:19:45 +02:00
|
|
|
}
|
|
|
|
while( strm.avail_out == 0 );
|
|
|
|
|
|
|
|
if( strm.avail_in != 0 )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate() - not all input is used" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( flush != Z_FINISH );
|
|
|
|
|
|
|
|
|
|
|
|
if( ret != Z_STREAM_END )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate() - stream not complete" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
|
2012-09-11 07:19:45 +02:00
|
|
|
void Compress::CopyToInputBuffer(BinaryPage::const_iterator & i, size_t len)
|
|
|
|
{
|
|
|
|
for(size_t a=0 ; a<len ; ++a, ++i)
|
|
|
|
buffer_in[a] = *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// new way
|
|
|
|
int Compress::MakeCompress(z_stream & strm, const BinaryPage & page, BinaryPage & out, int encoding)
|
|
|
|
{
|
|
|
|
int ret, flush;
|
|
|
|
size_t have;
|
|
|
|
BinaryPage::const_iterator i = page.begin();
|
|
|
|
|
|
|
|
size_t source_len = page.size();
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.avail_in = (source_len > buffer_max_len) ? buffer_max_len : source_len;
|
|
|
|
source_len -= strm.avail_in;
|
|
|
|
flush = (source_len == 0) ? Z_FINISH : Z_NO_FLUSH;
|
|
|
|
strm.next_in = (Bytef*)buffer_in;
|
2021-10-13 01:27:14 +02:00
|
|
|
|
|
|
|
// IMPROVE ME we can add an interface to pt::TextStreamBase<> and get all chunks of memory
|
2012-09-11 07:19:45 +02:00
|
|
|
CopyToInputBuffer(i, strm.avail_in);
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
strm.avail_out = buffer_max_len;
|
|
|
|
strm.next_out = (Bytef*)buffer_out;
|
2009-10-08 19:59:10 +02:00
|
|
|
ret = deflate(&strm, flush);
|
|
|
|
|
|
|
|
if( ret == Z_STREAM_ERROR || ret == Z_BUF_ERROR )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate()" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
have = buffer_max_len - strm.avail_out;
|
|
|
|
last_out_size += have;
|
2012-09-11 07:19:45 +02:00
|
|
|
out.write(buffer_out, have);
|
2009-10-08 19:59:10 +02:00
|
|
|
}
|
|
|
|
while( strm.avail_out == 0 );
|
|
|
|
|
|
|
|
if( strm.avail_in != 0 )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate() - not all input is used" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while( flush != Z_FINISH );
|
|
|
|
|
|
|
|
|
|
|
|
if( ret != Z_STREAM_END )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflate() - stream not complete" << logend;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-02 02:10:16 +02:00
|
|
|
z_stream * Compress::SelectStream(int encoding)
|
|
|
|
{
|
|
|
|
z_stream * pstrm;
|
|
|
|
|
|
|
|
if( encoding == 0 )
|
|
|
|
pstrm = &strm_raw_deflate;
|
|
|
|
else
|
|
|
|
if( encoding == 1 )
|
|
|
|
pstrm = &strm_deflate;
|
|
|
|
else
|
|
|
|
pstrm = &strm_gzip;
|
|
|
|
|
|
|
|
return pstrm;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Compress::ResetStream(z_stream * pstrm, int encoding)
|
|
|
|
{
|
|
|
|
if( deflateReset(pstrm) != Z_OK )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: problem with deflateReset()" << logend;
|
|
|
|
|
|
|
|
deflateEnd(pstrm);
|
|
|
|
|
|
|
|
if( encoding == 0 )
|
|
|
|
InitRawDeflate();
|
|
|
|
else
|
|
|
|
if( encoding == 1 )
|
|
|
|
InitDeflate();
|
|
|
|
else
|
|
|
|
InitGzip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Compress::PutLog(size_t source_len, int encoding)
|
|
|
|
{
|
|
|
|
double ratio = 100.0 - (double(last_out_size) / double(source_len) * 100.0);
|
|
|
|
char buffer[30];
|
|
|
|
sprintf(buffer, "%.1f", ratio);
|
|
|
|
|
|
|
|
log << log2 << "Compress: ";
|
|
|
|
|
|
|
|
if( encoding == 0 )
|
|
|
|
log << "raw deflate";
|
|
|
|
else
|
|
|
|
if( encoding == 1 )
|
|
|
|
log << "deflate";
|
|
|
|
else
|
|
|
|
log << "gzip";
|
|
|
|
|
|
|
|
log << ", original size: " << source_len << ", size after compressing: "
|
|
|
|
<< (int)last_out_size << ", ratio: " << buffer << "%" << logend;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
return:
|
|
|
|
0 - ok;
|
|
|
|
1 - can't allocate memory
|
|
|
|
2 - error during compressing
|
|
|
|
3 - not inited (use Init() first)
|
|
|
|
100 - unknown
|
|
|
|
*/
|
2013-12-04 02:21:57 +01:00
|
|
|
int Compress::Compressing(const char * source, size_t source_len, BinaryPage & out_stream, int encoding)
|
2009-10-08 19:59:10 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2011-04-02 02:10:16 +02:00
|
|
|
z_stream * pstrm;
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
last_out_size = 0;
|
2013-12-04 02:21:57 +01:00
|
|
|
out_stream.clear();
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
if( !ready_for_compress )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: not ready yet" << logend;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
// !! CHECK ME
|
|
|
|
// it is correct to immediately return? what about headers in the compressed page?
|
2009-10-08 19:59:10 +02:00
|
|
|
if( source_len == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2011-04-02 02:10:16 +02:00
|
|
|
pstrm = SelectStream(encoding);
|
|
|
|
ret = MakeCompress(*pstrm, source, source_len, out_stream, encoding);
|
|
|
|
ResetStream(pstrm, encoding);
|
|
|
|
PutLog(source_len, encoding);
|
2009-10-08 19:59:10 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-11 07:19:45 +02:00
|
|
|
/*
|
|
|
|
return:
|
|
|
|
0 - ok;
|
|
|
|
1 - can't allocate memory
|
|
|
|
2 - error during compressing
|
|
|
|
3 - not inited (use Init() first)
|
|
|
|
100 - unknown
|
|
|
|
*/
|
|
|
|
int Compress::Compressing(const BinaryPage & in, BinaryPage & out, int encoding)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
z_stream * pstrm;
|
|
|
|
|
|
|
|
last_out_size = 0;
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
if( !ready_for_compress )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: not ready yet" << logend;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2013-12-04 02:21:57 +01:00
|
|
|
// !! CHECK ME
|
|
|
|
// it is correct to immediately return? what about headers in the compressed page?
|
2012-09-11 07:19:45 +02:00
|
|
|
if( in.empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pstrm = SelectStream(encoding);
|
|
|
|
ret = MakeCompress(*pstrm, in, out, encoding);
|
|
|
|
ResetStream(pstrm, encoding);
|
|
|
|
PutLog(in.size(), encoding);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-12 17:30:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace Winix
|
|
|
|
|