compressionzlib.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "compressionzlib.h"
00016
00017 #ifdef HAVE_ZLIB
00018
00019 namespace gloox
00020 {
00021
00022 CompressionZlib::CompressionZlib( CompressionDataHandler *cdh )
00023 : CompressionBase( cdh )
00024 {
00025 int ret = Z_OK;
00026 m_zinflate.zalloc = Z_NULL;
00027 m_zinflate.zfree = Z_NULL;
00028 m_zinflate.opaque = Z_NULL;
00029 m_zinflate.avail_in = 0;
00030 m_zinflate.next_in = Z_NULL;
00031 ret = inflateInit( &m_zinflate );
00032
00033 if( ret == Z_OK )
00034 {
00035 m_zdeflate.zalloc = Z_NULL;
00036 m_zdeflate.zfree = Z_NULL;
00037 m_zdeflate.opaque = Z_NULL;
00038 m_zinflate.avail_in = 0;
00039 m_zinflate.next_in = Z_NULL;
00040 ret = deflateInit( &m_zdeflate, Z_BEST_COMPRESSION );
00041
00042 if( ret == Z_OK )
00043 m_valid = true;
00044 }
00045 }
00046
00047 CompressionZlib::~CompressionZlib()
00048 {
00049 inflateEnd( &m_zinflate );
00050 deflateEnd( &m_zdeflate );
00051 }
00052
00053 void CompressionZlib::compress( const std::string& data )
00054 {
00055 if( !m_valid || !m_handler || data.empty() )
00056 return;
00057
00058 m_compressMutex.lock();
00059
00060 int CHUNK = data.length() + ( data.length() / 100 ) + 13;
00061 Bytef *out = new Bytef[CHUNK];
00062 char *in = const_cast<char*>( data.c_str() );
00063
00064 m_zdeflate.avail_in = data.length();
00065 m_zdeflate.next_in = (Bytef*)in;
00066
00067 int ret;
00068 std::string result;
00069 do {
00070 m_zdeflate.avail_out = CHUNK;
00071 m_zdeflate.next_out = (Bytef*)out;
00072
00073 ret = deflate( &m_zdeflate, Z_SYNC_FLUSH );
00074 result.append( (char*)out, CHUNK - m_zdeflate.avail_out );
00075 } while( m_zdeflate.avail_out == 0 );
00076
00077 delete[] out;
00078
00079 m_compressMutex.unlock();
00080
00081 m_handler->handleCompressedData( result );
00082 }
00083
00084 void CompressionZlib::decompress( const std::string& data )
00085 {
00086 if( !m_valid || !m_handler || data.empty() )
00087 return;
00088
00089 int CHUNK = 50;
00090 char *out = new char[CHUNK];
00091 char *in = const_cast<char*>( data.c_str() );
00092
00093 m_zinflate.avail_in = data.length();
00094 m_zinflate.next_in = (Bytef*)in;
00095
00096 int ret = Z_OK;
00097 std::string result;
00098 do
00099 {
00100 m_zinflate.avail_out = CHUNK;
00101 m_zinflate.next_out = (Bytef*)out;
00102
00103 ret = inflate( &m_zinflate, Z_SYNC_FLUSH );
00104 result.append( out, CHUNK - m_zinflate.avail_out );
00105 } while( m_zinflate.avail_out == 0 );
00106
00107 delete[] out;
00108
00109 m_handler->handleDecompressedData( result );
00110 }
00111
00112 }
00113
00114 #endif // HAVE_ZLIB