2009-09-22 01:05:05 +02:00
|
|
|
/*
|
2010-02-28 01:08:10 +01:00
|
|
|
* This file is a part of Winix
|
2009-09-22 01:05:05 +02:00
|
|
|
* and is not publicly distributed
|
|
|
|
*
|
2011-04-02 02:10:16 +02:00
|
|
|
* Copyright (c) 2008-2011, Tomasz Sowa
|
2009-09-22 01:05:05 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
#include "compress.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Compress::Compress()
|
|
|
|
{
|
2011-04-02 02:10:16 +02:00
|
|
|
buffer = 0;
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
if( buffer )
|
|
|
|
delete [] buffer;
|
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()
|
|
|
|
{
|
|
|
|
if( buffer )
|
|
|
|
delete [] buffer;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
buffer = new char[buffer_max_len];
|
|
|
|
}
|
|
|
|
catch(const std::bad_alloc &)
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: can't allocate memory" << logend;
|
|
|
|
buffer = 0;
|
|
|
|
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_;
|
|
|
|
|
2009-10-08 19:59:10 +02:00
|
|
|
if( buffer == 0 )
|
|
|
|
if( !AllocateMemory() )
|
|
|
|
return 1;
|
|
|
|
|
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
|
|
|
|
|
|
|
int Compress::MakeCompress(z_stream & strm, const char * source, size_t source_len, FCGX_Stream * 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;
|
|
|
|
strm.next_out = (Bytef*)buffer;
|
|
|
|
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;
|
|
|
|
FCGX_PutStr(buffer, have, out_stream);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2011-04-02 02:10:16 +02:00
|
|
|
int Compress::CompressAndPut(const char * source, size_t source_len, FCGX_Stream * 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;
|
|
|
|
|
|
|
|
if( !ready_for_compress )
|
|
|
|
{
|
|
|
|
log << log1 << "Compress: not ready yet" << logend;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|