kioslave
kbzip2filter.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022
00023 #if defined( HAVE_BZIP2_SUPPORT )
00024
00025
00026 #define BZ_NO_STDIO
00027 extern "C" {
00028 #include <bzlib.h>
00029 }
00030
00031 #ifdef NEED_BZ2_PREFIX
00032 #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z)
00033 #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x)
00034 #define bzCompressEnd(x) BZ2_bzCompressEnd(x)
00035 #define bzDecompress(x) BZ2_bzDecompress(x)
00036 #define bzCompress(x,y) BZ2_bzCompress(x, y)
00037 #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a);
00038 #endif
00039
00040 #include <kdebug.h>
00041 #include <klibloader.h>
00042
00043 #include "kbzip2filter.h"
00044
00045
00046
00047 class KBzip2FilterFactory : public KLibFactory
00048 {
00049 public:
00050 KBzip2FilterFactory() : KLibFactory() {}
00051 virtual ~KBzip2FilterFactory(){}
00052 QObject *createObject( QObject *, const char *, const char*, const QStringList & )
00053 {
00054 return new KBzip2Filter;
00055 }
00056 };
00057
00058 K_EXPORT_COMPONENT_FACTORY( kbzip2filter, KBzip2FilterFactory )
00059
00060
00061 class KBzip2Filter::KBzip2FilterPrivate
00062 {
00063 public:
00064 bz_stream zStream;
00065 };
00066
00067 KBzip2Filter::KBzip2Filter()
00068 {
00069 d = new KBzip2FilterPrivate;
00070 d->zStream.bzalloc = 0;
00071 d->zStream.bzfree = 0;
00072 d->zStream.opaque = 0;
00073 m_mode = 0;
00074 }
00075
00076
00077 KBzip2Filter::~KBzip2Filter()
00078 {
00079 delete d;
00080 }
00081
00082 void KBzip2Filter::init( int mode )
00083 {
00084 d->zStream.next_in = 0;
00085 d->zStream.avail_in = 0;
00086 if ( mode == IO_ReadOnly )
00087 {
00088 (void)bzDecompressInit(&d->zStream, 0, 0);
00089
00090
00091 } else if ( mode == IO_WriteOnly ) {
00092 (void)bzCompressInit(&d->zStream, 5, 0, 0);
00093
00094 } else
00095 kdWarning(7118) << "Unsupported mode " << mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
00096 m_mode = mode;
00097 }
00098
00099 void KBzip2Filter::terminate()
00100 {
00101 if ( m_mode == IO_ReadOnly )
00102 {
00103 int result = bzDecompressEnd(&d->zStream);
00104 kdDebug(7118) << "bzDecompressEnd returned " << result << endl;
00105 } else if ( m_mode == IO_WriteOnly )
00106 {
00107 int result = bzCompressEnd(&d->zStream);
00108 kdDebug(7118) << "bzCompressEnd returned " << result << endl;
00109 } else
00110 kdWarning(7118) << "Unsupported mode " << m_mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
00111 }
00112
00113
00114 void KBzip2Filter::reset()
00115 {
00116 kdDebug(7118) << "KBzip2Filter::reset" << endl;
00117
00118 terminate();
00119 init( m_mode );
00120 }
00121
00122 void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
00123 {
00124 d->zStream.avail_out = maxlen;
00125 d->zStream.next_out = data;
00126 }
00127
00128 void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
00129 {
00130 d->zStream.avail_in = size;
00131 d->zStream.next_in = const_cast<char *>(data);
00132 }
00133
00134 int KBzip2Filter::inBufferAvailable() const
00135 {
00136 return d->zStream.avail_in;
00137 }
00138
00139 int KBzip2Filter::outBufferAvailable() const
00140 {
00141 return d->zStream.avail_out;
00142 }
00143
00144 KBzip2Filter::Result KBzip2Filter::uncompress()
00145 {
00146
00147 int result = bzDecompress(&d->zStream);
00148 if ( result != BZ_OK )
00149 {
00150 kdDebug(7118) << "bzDecompress returned " << result << endl;
00151 kdDebug(7118) << "KBzip2Filter::uncompress " << ( result == BZ_OK ? OK : ( result == BZ_STREAM_END ? END : ERROR ) ) << endl;
00152 }
00153
00154 switch (result) {
00155 case BZ_OK:
00156 return OK;
00157 case BZ_STREAM_END:
00158 return END;
00159 default:
00160 return ERROR;
00161 }
00162 }
00163
00164 KBzip2Filter::Result KBzip2Filter::compress( bool finish )
00165 {
00166
00167 int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
00168
00169 switch (result) {
00170 case BZ_OK:
00171 case BZ_FLUSH_OK:
00172 case BZ_RUN_OK:
00173 case BZ_FINISH_OK:
00174 return OK;
00175 break;
00176 case BZ_STREAM_END:
00177 kdDebug(7118) << " bzCompress returned " << result << endl;
00178 return END;
00179 break;
00180 default:
00181 kdDebug(7118) << " bzCompress returned " << result << endl;
00182 return ERROR;
00183 break;
00184 }
00185 }
00186
00187 #endif