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-compression.h> 00023 00024 #if 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 #if 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 <QDebug> 00044 00045 #include <qiodevice.h> 00046 00047 00048 00049 // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html 00050 00051 class KBzip2Filter::Private 00052 { 00053 public: 00054 Private() 00055 : isInitialized(false) 00056 { 00057 memset(&zStream, 0, sizeof(zStream)); 00058 mode = 0; 00059 } 00060 00061 bz_stream zStream; 00062 int mode; 00063 bool isInitialized; 00064 }; 00065 00066 KBzip2Filter::KBzip2Filter() 00067 :d(new Private) 00068 { 00069 } 00070 00071 00072 KBzip2Filter::~KBzip2Filter() 00073 { 00074 delete d; 00075 } 00076 00077 void KBzip2Filter::init( int mode ) 00078 { 00079 if (d->isInitialized) { 00080 terminate(); 00081 } 00082 00083 d->zStream.next_in = 0; 00084 d->zStream.avail_in = 0; 00085 if ( mode == QIODevice::ReadOnly ) 00086 { 00087 (void)bzDecompressInit(&d->zStream, 0, 0); 00088 //qDebug() << "bzDecompressInit returned " << result; 00089 // TODO: test result and return false on error 00090 } else if ( mode == QIODevice::WriteOnly ) { 00091 (void)bzCompressInit(&d->zStream, 5, 0, 0); 00092 //qDebug() << "bzDecompressInit returned " << result; 00093 // TODO: test result and return false on error 00094 } else { 00095 qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported"; 00096 // TODO return false 00097 } 00098 d->mode = mode; 00099 d->isInitialized = true; 00100 } 00101 00102 int KBzip2Filter::mode() const 00103 { 00104 return d->mode; 00105 } 00106 00107 void KBzip2Filter::terminate() 00108 { 00109 if (d->mode == QIODevice::ReadOnly) { 00110 int result = bzDecompressEnd(&d->zStream); 00111 // TODO: test result and return false on error 00112 //qDebug() << "bzDecompressEnd returned " << result; 00113 } else if (d->mode == QIODevice::WriteOnly) { 00114 int result = bzCompressEnd(&d->zStream); 00115 // TODO: test result and return false on error 00116 //qDebug() << "bzCompressEnd returned " << result; 00117 } else { 00118 qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported"; 00119 // TODO return false 00120 } 00121 d->isInitialized = false; 00122 } 00123 00124 00125 void KBzip2Filter::reset() 00126 { 00127 // bzip2 doesn't seem to have a reset call... 00128 terminate(); 00129 init( d->mode ); 00130 } 00131 00132 void KBzip2Filter::setOutBuffer( char * data, uint maxlen ) 00133 { 00134 d->zStream.avail_out = maxlen; 00135 d->zStream.next_out = data; 00136 } 00137 00138 void KBzip2Filter::setInBuffer( const char *data, unsigned int size ) 00139 { 00140 d->zStream.avail_in = size; 00141 d->zStream.next_in = const_cast<char *>(data); 00142 } 00143 00144 int KBzip2Filter::inBufferAvailable() const 00145 { 00146 return d->zStream.avail_in; 00147 } 00148 00149 int KBzip2Filter::outBufferAvailable() const 00150 { 00151 return d->zStream.avail_out; 00152 } 00153 00154 KBzip2Filter::Result KBzip2Filter::uncompress() 00155 { 00156 //qDebug() << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable(); 00157 int result = bzDecompress(&d->zStream); 00158 if ( result < BZ_OK ) 00159 { 00160 kWarning() << "bzDecompress returned" << result; 00161 } 00162 00163 switch (result) { 00164 case BZ_OK: 00165 return KFilterBase::Ok; 00166 case BZ_STREAM_END: 00167 return KFilterBase::End; 00168 default: 00169 return KFilterBase::Error; 00170 } 00171 } 00172 00173 KBzip2Filter::Result KBzip2Filter::compress( bool finish ) 00174 { 00175 //qDebug() << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable(); 00176 int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN ); 00177 00178 switch (result) { 00179 case BZ_OK: 00180 case BZ_FLUSH_OK: 00181 case BZ_RUN_OK: 00182 case BZ_FINISH_OK: 00183 return KFilterBase::Ok; 00184 break; 00185 case BZ_STREAM_END: 00186 //qDebug() << " bzCompress returned " << result; 00187 return KFilterBase::End; 00188 break; 00189 default: 00190 //qDebug() << " bzCompress returned " << result; 00191 return KFilterBase::Error; 00192 break; 00193 } 00194 } 00195 00196 #endif /* HAVE_BZIP2_SUPPORT */
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:28:08 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:28:08 by doxygen 1.7.5.1 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.