klzmafilter.cpp00001
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_LZMA_SUPPORT )
00024
00025 #define LZMADEC_NO_STDIO
00026 #include <sys/types.h>
00027 extern "C" {
00028 #include <lzmadec.h>
00029 }
00030
00031 #include <kdebug.h>
00032 #include <klibloader.h>
00033
00034 #include "klzmafilter.h"
00035
00036 class KLzmaFilterFactory : public KLibFactory
00037 {
00038 public:
00039 KLzmaFilterFactory() : KLibFactory() {}
00040 virtual ~KLzmaFilterFactory(){}
00041 QObject *createObject( QObject *, const char *, const char*, const QStringList & )
00042 {
00043 return new KLzmaFilter;
00044 }
00045 };
00046
00047 K_EXPORT_COMPONENT_FACTORY( klzmafilter, KLzmaFilterFactory )
00048
00049
00050 class KLzmaFilter::KLzmaFilterPrivate
00051 {
00052 public:
00053 lzmadec_stream zStream;
00054 };
00055
00056 KLzmaFilter::KLzmaFilter()
00057 {
00058 d = new KLzmaFilterPrivate;
00059 d->zStream.lzma_alloc = 0;
00060 d->zStream.lzma_free = 0;
00061 d->zStream.opaque = 0;
00062 m_mode = 0;
00063 }
00064
00065
00066 KLzmaFilter::~KLzmaFilter()
00067 {
00068 delete d;
00069 }
00070
00071 void KLzmaFilter::init( int mode )
00072 {
00073 d->zStream.next_in = 0;
00074 d->zStream.avail_in = 0;
00075 if ( mode == IO_ReadOnly )
00076 {
00077
00078
00079
00080
00081 (void)lzmadec_init(&d->zStream);
00082 } else
00083 kdWarning(7129) << "Unsupported mode " << mode << ". Only IO_ReadOnly supported" << endl;
00084 m_mode = mode;
00085 m_finishing = false;
00086 }
00087
00088 void KLzmaFilter::terminate()
00089 {
00090 if ( m_mode == IO_ReadOnly )
00091 {
00092 int result = lzmadec_end(&d->zStream);
00093 kdDebug(7129) << "lzmadec_end returned " << result << endl;
00094 } else
00095 kdWarning(7129) << "Unsupported mode " << m_mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
00096 }
00097
00098
00099 void KLzmaFilter::reset()
00100 {
00101 kdDebug(7129) << "KLzmaFilter::reset" << endl;
00102
00103 terminate();
00104 init( m_mode );
00105 }
00106
00107 void KLzmaFilter::setOutBuffer( char * data, uint maxlen )
00108 {
00109 d->zStream.avail_out = maxlen;
00110 d->zStream.next_out = (uint8_t *) data;
00111 }
00112
00113 void KLzmaFilter::setInBuffer( const char *data, unsigned int size )
00114 {
00115 if (size == 0)
00116 m_finishing = true;
00117
00118 d->zStream.avail_in = size;
00119 d->zStream.next_in = (uint8_t *)const_cast<char *>(data);
00120 }
00121
00122 int KLzmaFilter::inBufferAvailable() const
00123 {
00124 return d->zStream.avail_in;
00125 }
00126
00127 int KLzmaFilter::outBufferAvailable() const
00128 {
00129 return d->zStream.avail_out;
00130 }
00131
00132 KLzmaFilter::Result KLzmaFilter::uncompress()
00133 {
00134 int result = lzmadec_decode(&d->zStream, m_finishing);
00135 if ( result != LZMADEC_OK )
00136 {
00137 kdDebug(7129) << "lzmadec_decode returned " << result << endl;
00138 kdDebug(7129) << "KLzmaFilter::uncompress " << ( result == LZMADEC_OK ? OK : ( result == LZMADEC_STREAM_END ? END : ERROR ) ) << endl;
00139 }
00140
00141 switch (result) {
00142 case LZMADEC_OK:
00143 return OK;
00144 case LZMADEC_STREAM_END:
00145 return END;
00146 default:
00147 return ERROR;
00148 }
00149 }
00150
00151 KLzmaFilter::Result KLzmaFilter::compress( bool finish )
00152 {
00153 kdError(7129) << "lzmadec doesn't support compression!" << finish << endl;
00154 return ERROR;
00155 }
00156 #endif
|