00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "compression.h"
00016
00017 namespace gloox
00018 {
00019
00020 Compression::Compression( StreamFeature method )
00021 : m_valid( false ), m_method( method ), m_compCount( 0 ), m_decompCount( 0 ), m_dataOutCount( 0 ),
00022 m_dataInCount( 0 )
00023 {
00024 switch( method )
00025 {
00026 case StreamFeatureCompressZlib:
00027 {
00028 #ifdef HAVE_ZLIB
00029 int ret = Z_OK;
00030 m_zinflate.zalloc = Z_NULL;
00031 m_zinflate.zfree = Z_NULL;
00032 m_zinflate.opaque = Z_NULL;
00033 m_zinflate.avail_in = 0;
00034 m_zinflate.next_in = Z_NULL;
00035 ret = inflateInit( &m_zinflate );
00036
00037 if( ret == Z_OK )
00038 {
00039 m_zdeflate.zalloc = Z_NULL;
00040 m_zdeflate.zfree = Z_NULL;
00041 m_zdeflate.opaque = Z_NULL;
00042 m_zinflate.avail_in = 0;
00043 m_zinflate.next_in = Z_NULL;
00044 ret = deflateInit( &m_zdeflate, Z_BEST_COMPRESSION );
00045
00046 if( ret == Z_OK )
00047 m_valid = true;
00048 }
00049 #endif
00050 break;
00051 }
00052 case StreamFeatureCompressDclz:
00053 {
00054 #ifdef HAVE_LZW
00055
00056 #endif
00057 break;
00058 }
00059 default:
00060 break;
00061 }
00062 }
00063
00064 Compression::~Compression()
00065 {
00066 #ifdef HAVE_ZLIB
00067 inflateEnd( &m_zinflate );
00068 deflateEnd( &m_zdeflate );
00069 #endif
00070 #ifdef HAVE_LZW
00071
00072 #endif
00073 }
00074
00075 const std::string Compression::compress( const std::string& data )
00076 {
00077 if( !m_valid )
00078 return data;
00079
00080 if( data.empty() )
00081 return "";
00082
00083 #ifdef HAVE_ZLIB
00084 int CHUNK = data.length() + ( data.length() / 100 ) + 13;
00085 Bytef *out = new Bytef[CHUNK];
00086 char *in = const_cast<char*>( data.c_str() );
00087
00088 m_zdeflate.avail_in = data.length();
00089 m_zdeflate.next_in = (Bytef*)in;
00090
00091 int ret;
00092 std::string result, tmp;
00093 do {
00094 m_zdeflate.avail_out = CHUNK;
00095 m_zdeflate.next_out = (Bytef*)out;
00096
00097 ret = deflate( &m_zdeflate, Z_SYNC_FLUSH );
00098 tmp.assign( (char*)out, CHUNK - m_zdeflate.avail_out );
00099 result += tmp;
00100 } while( m_zdeflate.avail_out == 0 );
00101
00102 m_compCount += result.length();
00103 m_dataOutCount += data.length();
00104 delete[] out;
00105
00106 return result;
00107 #else
00108 return data;
00109 #endif
00110 }
00111
00112 const std::string Compression::decompress( const std::string& data )
00113 {
00114 if( !m_valid )
00115 return data;
00116
00117 if( data.empty() )
00118 return "";
00119
00120 #ifdef HAVE_ZLIB
00121 m_inflateBuffer += data;
00122
00123 int CHUNK = 50;
00124 char *out = new char[CHUNK];
00125 char *in = const_cast<char*>( m_inflateBuffer.c_str() );
00126
00127 m_zinflate.avail_in = m_inflateBuffer.length();
00128 m_zinflate.next_in = (Bytef*)in;
00129
00130 int ret = Z_OK;
00131 std::string result, tmp;
00132 do
00133 {
00134 m_zinflate.avail_out = CHUNK;
00135 m_zinflate.next_out = (Bytef*)out;
00136
00137 ret = inflate( &m_zinflate, Z_SYNC_FLUSH );
00138 tmp.assign( out, CHUNK - m_zinflate.avail_out );
00139 result += tmp;
00140 } while( m_zinflate.avail_out == 0 );
00141
00142 m_decompCount += result.length();
00143 m_dataInCount += m_inflateBuffer.length();
00144 delete[] out;
00145
00146 m_inflateBuffer.clear();
00147 return result;
00148 #else
00149 return data;
00150 #endif
00151 }
00152
00153 }