• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kxzfilter.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2007-2008 Per Øyvind Karlsen <peroyvind@mandriva.org>
00003 
00004    Based on kbzip2filter:
00005    Copyright (C) 2000-2005 David Faure <faure@kde.org>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public License
00018    along with this library; see the file COPYING.LIB.  If not, write to
00019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020    Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "kxzfilter.h"
00024 
00025 #include <config.h>
00026 
00027 #if defined( HAVE_XZ_SUPPORT )
00028 extern "C" {
00029     #include <lzma.h>
00030 }
00031 
00032 #include <kdebug.h>
00033 
00034 #include <qiodevice.h>
00035 
00036 
00037 class KXzFilter::Private
00038 {
00039 public:
00040     Private()
00041     {
00042         memset(&zStream, 0, sizeof(zStream));
00043         mode = 0;
00044     }
00045 
00046     lzma_stream zStream;
00047     int mode;
00048 };
00049 
00050 KXzFilter::KXzFilter()
00051     :d(new Private)
00052 {
00053 }
00054 
00055 
00056 KXzFilter::~KXzFilter()
00057 {
00058     delete d;
00059 }
00060 
00061 void KXzFilter::init( int mode )
00062 {
00063     lzma_ret result;
00064     d->zStream.next_in = 0;
00065     d->zStream.avail_in = 0;
00066     if ( mode == QIODevice::ReadOnly )
00067     {
00068     /* We set the memlimit for decompression to 100MiB which should be
00069      * more than enough to be sufficient for level 9 which requires 65 MiB.
00070      */
00071         result = lzma_auto_decoder(&d->zStream, 100<<20, 0);
00072         //kDebug(7131) << "lzma_auto_decoder returned " << result;
00073     } else if ( mode == QIODevice::WriteOnly ) {
00074         result = lzma_easy_encoder(&d->zStream, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC32);
00075         //kDebug(7131) << "lzma_easy_encoder returned " << result;
00076     } else
00077         kWarning(7131) << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00078     d->mode = mode;
00079 }
00080 
00081 int KXzFilter::mode() const
00082 {
00083     return d->mode;
00084 }
00085 
00086 void KXzFilter::terminate()
00087 {
00088     if ( d->mode == QIODevice::ReadOnly || d->mode == QIODevice::WriteOnly )
00089     {
00090         lzma_end(&d->zStream);
00091     } else
00092         kWarning(7131) << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
00093 }
00094 
00095 
00096 void KXzFilter::reset()
00097 {
00098     kDebug(7131) << "KXzFilter::reset";
00099     // liblzma doesn't have a reset call...
00100     terminate();
00101     init( d->mode );
00102 }
00103 
00104 void KXzFilter::setOutBuffer( char * data, uint maxlen )
00105 {
00106     d->zStream.avail_out = maxlen;
00107     d->zStream.next_out = (uint8_t *)data;
00108 }
00109 
00110 void KXzFilter::setInBuffer( const char *data, unsigned int size )
00111 {
00112     d->zStream.avail_in = size;
00113     d->zStream.next_in = (uint8_t *)const_cast<char *>(data);
00114 }
00115 
00116 int KXzFilter::inBufferAvailable() const
00117 {
00118     return d->zStream.avail_in;
00119 }
00120 
00121 int KXzFilter::outBufferAvailable() const
00122 {
00123     return d->zStream.avail_out;
00124 }
00125 
00126 KXzFilter::Result KXzFilter::uncompress()
00127 {
00128     //kDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out =" << outBufferAvailable();
00129     lzma_ret result = lzma_code(&d->zStream, LZMA_RUN);
00130     if ( result != LZMA_OK )
00131     {
00132         kDebug(7131) << "lzma_code returned " << result;
00133         kDebug(7131) << "KXzFilter::uncompress " << ( result == LZMA_STREAM_END ? KFilterBase::End : KFilterBase::Error );
00134     }
00135 
00136     switch (result) {
00137         case LZMA_OK:
00138                 return KFilterBase::Ok;
00139         case LZMA_STREAM_END:
00140                 return KFilterBase::End;
00141         default:
00142                 return KFilterBase::Error;
00143     }
00144 }
00145 
00146 KXzFilter::Result KXzFilter::compress( bool finish )
00147 {
00148     //kDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
00149     lzma_ret result = lzma_code(&d->zStream, finish ? LZMA_FINISH : LZMA_RUN );
00150 
00151     switch (result) {
00152         case LZMA_OK:
00153                 return KFilterBase::Ok;
00154                 break;
00155         case LZMA_STREAM_END:
00156                 kDebug(7131) << "  lzma_code returned " << result;
00157                 return KFilterBase::End;
00158         break;
00159         default:
00160                 kDebug(7131) << "  lzma_code returned " << result;
00161                 return KFilterBase::Error;
00162                 break;
00163     }
00164 }
00165 
00166 #endif  /* HAVE_XZ_SUPPORT */

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal