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         // Ignoring error like kgzipfilter and kbzip2filter.
00078         // Like with zlib and libbzip2, we will safely fail if we
00079     // have liblzmadec 27dc3a939f64f97a4f02bd9d06b1e901e90f3571
00080     // or later (will be in LZMA Utils 4.32.4).
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     // lzma doesn't seem to have a reset call...
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
KDE Home | KDE Accessibility Home | Description of Access Keys