klzmafilter.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003    Copyright (C) 2007 Per Øyvind Karlsen <peroyvind@mandriva.org>
00004 
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License version 2 as published by the Free Software Foundation.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <config.h>
00022 
00023 #if defined( HAVE_LZMA_SUPPORT )
00024 // we don't need that
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 // Not really useful anymore
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         (void)lzmadec_init(&d->zStream);
00078     } else
00079         kdWarning(7129) << "Unsupported mode " << mode << ". Only IO_ReadOnly supported" << endl;
00080     m_mode = mode;
00081 }
00082 
00083 void KLzmaFilter::terminate()
00084 {
00085     if ( m_mode == IO_ReadOnly )
00086     {
00087         int result = lzmadec_end(&d->zStream);
00088         kdDebug(7129) << "lzmadec_end returned " << result << endl;
00089     } else
00090         kdWarning(7129) << "Unsupported mode " << m_mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
00091 }
00092 
00093 
00094 void KLzmaFilter::reset()
00095 {
00096     kdDebug(7129) << "KLzmaFilter::reset" << endl;
00097     // lzma doesn't seem to have a reset call...
00098     terminate();
00099     init( m_mode );
00100 }
00101 
00102 void KLzmaFilter::setOutBuffer( char * data, uint maxlen )
00103 {
00104     d->zStream.avail_out = maxlen;
00105     d->zStream.next_out = (uint8_t *) data;
00106 }
00107 
00108 void KLzmaFilter::setInBuffer( const char *data, unsigned int size )
00109 {
00110     d->zStream.avail_in = size;
00111     d->zStream.next_in = (uint8_t *)const_cast<char *>(data);
00112 }
00113 
00114 int KLzmaFilter::inBufferAvailable() const
00115 {
00116     return d->zStream.avail_in;
00117 }
00118 
00119 int KLzmaFilter::outBufferAvailable() const
00120 {
00121     return d->zStream.avail_out;
00122 }
00123 
00124 KLzmaFilter::Result KLzmaFilter::uncompress()
00125 {
00126     int result = lzmadec_decode(&d->zStream, 1);
00127     if ( result != LZMADEC_OK )
00128     {
00129         kdDebug(7129) << "lzmadec_decode returned " << result << endl;
00130         kdDebug(7129) << "KLzmaFilter::uncompress " << ( result == LZMADEC_OK ? OK : ( result == LZMADEC_STREAM_END ? END : ERROR ) ) << endl;
00131     }
00132 
00133     switch (result) {
00134         case LZMADEC_OK:
00135                 return OK;
00136         case LZMADEC_STREAM_END:
00137                 return END;
00138         default:
00139                 return ERROR;
00140     }
00141 }
00142 
00143 KLzmaFilter::Result KLzmaFilter::compress( bool finish )
00144 {
00145     kdError(7129) << "lzmadec doesn't support compression!" << finish << endl;  
00146     return ERROR;
00147 }   
00148 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys