KDECore
kbzip2filter.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2000-2005 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kbzip2filter.h" 00021 00022 #include <config.h> 00023 00024 #if defined( HAVE_BZIP2_SUPPORT ) 00025 00026 // we don't need that 00027 #define BZ_NO_STDIO 00028 extern "C" { 00029 #include <bzlib.h> 00030 } 00031 00032 #ifdef NEED_BZ2_PREFIX 00033 #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z) 00034 #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x) 00035 #define bzCompressEnd(x) BZ2_bzCompressEnd(x) 00036 #define bzDecompress(x) BZ2_bzDecompress(x) 00037 #define bzCompress(x,y) BZ2_bzCompress(x, y) 00038 #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a); 00039 #endif 00040 00041 #include <kdebug.h> 00042 00043 #include <qiodevice.h> 00044 00045 00046 00047 // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html 00048 00049 class KBzip2Filter::Private 00050 { 00051 public: 00052 Private() 00053 : isInitialized(false) 00054 { 00055 memset(&zStream, 0, sizeof(zStream)); 00056 mode = 0; 00057 } 00058 00059 bz_stream zStream; 00060 int mode; 00061 bool isInitialized; 00062 }; 00063 00064 KBzip2Filter::KBzip2Filter() 00065 :d(new Private) 00066 { 00067 } 00068 00069 00070 KBzip2Filter::~KBzip2Filter() 00071 { 00072 delete d; 00073 } 00074 00075 void KBzip2Filter::init( int mode ) 00076 { 00077 if (d->isInitialized) { 00078 terminate(); 00079 } 00080 00081 d->zStream.next_in = 0; 00082 d->zStream.avail_in = 0; 00083 if ( mode == QIODevice::ReadOnly ) 00084 { 00085 (void)bzDecompressInit(&d->zStream, 0, 0); 00086 //kDebug(7118) << "bzDecompressInit returned " << result; 00087 // No idea what to do with result :) 00088 } else if ( mode == QIODevice::WriteOnly ) { 00089 (void)bzCompressInit(&d->zStream, 5, 0, 0); 00090 //kDebug(7118) << "bzDecompressInit returned " << result; 00091 } else 00092 kWarning(7118) << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported"; 00093 d->mode = mode; 00094 d->isInitialized = true; 00095 } 00096 00097 int KBzip2Filter::mode() const 00098 { 00099 return d->mode; 00100 } 00101 00102 void KBzip2Filter::terminate() 00103 { 00104 if (d->mode == QIODevice::ReadOnly) { 00105 int result = bzDecompressEnd(&d->zStream); 00106 kDebug(7118) << "bzDecompressEnd returned " << result; 00107 } else if (d->mode == QIODevice::WriteOnly) { 00108 int result = bzCompressEnd(&d->zStream); 00109 kDebug(7118) << "bzCompressEnd returned " << result; 00110 } else { 00111 kWarning(7118) << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported"; 00112 } 00113 d->isInitialized = false; 00114 } 00115 00116 00117 void KBzip2Filter::reset() 00118 { 00119 kDebug(7118) << "KBzip2Filter::reset"; 00120 // bzip2 doesn't seem to have a reset call... 00121 terminate(); 00122 init( d->mode ); 00123 } 00124 00125 void KBzip2Filter::setOutBuffer( char * data, uint maxlen ) 00126 { 00127 d->zStream.avail_out = maxlen; 00128 d->zStream.next_out = data; 00129 } 00130 00131 void KBzip2Filter::setInBuffer( const char *data, unsigned int size ) 00132 { 00133 d->zStream.avail_in = size; 00134 d->zStream.next_in = const_cast<char *>(data); 00135 } 00136 00137 int KBzip2Filter::inBufferAvailable() const 00138 { 00139 return d->zStream.avail_in; 00140 } 00141 00142 int KBzip2Filter::outBufferAvailable() const 00143 { 00144 return d->zStream.avail_out; 00145 } 00146 00147 KBzip2Filter::Result KBzip2Filter::uncompress() 00148 { 00149 //kDebug(7118) << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable(); 00150 int result = bzDecompress(&d->zStream); 00151 if ( result != BZ_OK ) 00152 { 00153 kDebug(7118) << "bzDecompress returned " << result; 00154 kDebug(7118) << "KBzip2Filter::uncompress " << ( result == BZ_STREAM_END ? KFilterBase::End : KFilterBase::Error ); 00155 } 00156 00157 switch (result) { 00158 case BZ_OK: 00159 return KFilterBase::Ok; 00160 case BZ_STREAM_END: 00161 return KFilterBase::End; 00162 default: 00163 return KFilterBase::Error; 00164 } 00165 } 00166 00167 KBzip2Filter::Result KBzip2Filter::compress( bool finish ) 00168 { 00169 //kDebug(7118) << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable(); 00170 int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN ); 00171 00172 switch (result) { 00173 case BZ_OK: 00174 case BZ_FLUSH_OK: 00175 case BZ_RUN_OK: 00176 case BZ_FINISH_OK: 00177 return KFilterBase::Ok; 00178 break; 00179 case BZ_STREAM_END: 00180 kDebug(7118) << " bzCompress returned " << result; 00181 return KFilterBase::End; 00182 break; 00183 default: 00184 kDebug(7118) << " bzCompress returned " << result; 00185 return KFilterBase::Error; 00186 break; 00187 } 00188 } 00189 00190 #endif /* HAVE_BZIP2_SUPPORT */
KDE 4.6 API Reference