|
Line 0
Link Here
|
|
|
1 |
/* This file is part of the KDE libraries |
| 2 |
Copyright (C) 2007-2008 Per Øyvind Karlsen <peroyvind@mandriva.org> |
| 3 |
|
| 4 |
Based on kbzip2filter: |
| 5 |
Copyright (C) 2000-2005 David Faure <faure@kde.org> |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Library General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 2 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
This library is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
Library General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Library General Public License |
| 18 |
along with this library; see the file COPYING.LIB. If not, write to |
| 19 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 |
Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kxzfilter.h" |
| 24 |
#include <klibloader.h> |
| 25 |
|
| 26 |
//#include <config-compression.h> |
| 27 |
#define HAVE_XZ_SUPPORT 1 |
| 28 |
#if HAVE_XZ_SUPPORT |
| 29 |
extern "C" { |
| 30 |
#include <lzma.h> |
| 31 |
} |
| 32 |
|
| 33 |
#include <kdebug.h> |
| 34 |
|
| 35 |
#include <qiodevice.h> |
| 36 |
|
| 37 |
// #define DEBUG_XZ |
| 38 |
|
| 39 |
class KXzFilterFactory : public KLibFactory |
| 40 |
{ |
| 41 |
public: |
| 42 |
KXzFilterFactory() : KLibFactory() {} |
| 43 |
virtual ~KXzFilterFactory(){} |
| 44 |
TQObject *createObject( TQObject *, const char *, const char*, const TQStringList & ) |
| 45 |
{ |
| 46 |
return new KXzFilter; |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
K_EXPORT_COMPONENT_FACTORY( kxzfilter, KXzFilterFactory ) |
| 51 |
|
| 52 |
|
| 53 |
class KXzFilter::Private |
| 54 |
{ |
| 55 |
public: |
| 56 |
Private() |
| 57 |
: isInitialized(false) |
| 58 |
{ |
| 59 |
memset(&zStream, 0, sizeof(zStream)); |
| 60 |
mode = 0; |
| 61 |
} |
| 62 |
|
| 63 |
lzma_stream zStream; |
| 64 |
int mode; |
| 65 |
bool isInitialized; |
| 66 |
}; |
| 67 |
|
| 68 |
KXzFilter::KXzFilter() |
| 69 |
:d(new Private) |
| 70 |
{ |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
KXzFilter::~KXzFilter() |
| 75 |
{ |
| 76 |
delete d; |
| 77 |
} |
| 78 |
|
| 79 |
void KXzFilter::init( int mode ) |
| 80 |
{ |
| 81 |
if (d->isInitialized) { |
| 82 |
terminate(); |
| 83 |
} |
| 84 |
|
| 85 |
lzma_ret result; |
| 86 |
d->zStream.next_in = 0; |
| 87 |
d->zStream.avail_in = 0; |
| 88 |
if ( mode == IO_ReadOnly ) |
| 89 |
{ |
| 90 |
/* We set the memlimit for decompression to 100MiB which should be |
| 91 |
* more than enough to be sufficient for level 9 which requires 65 MiB. |
| 92 |
*/ |
| 93 |
result = lzma_auto_decoder(&d->zStream, 100<<20, 0); |
| 94 |
//kdDebug(7131) << "lzma_auto_decoder returned " << result; |
| 95 |
} else if ( mode == IO_WriteOnly ) { |
| 96 |
result = lzma_easy_encoder(&d->zStream, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC32); |
| 97 |
//kdDebug(7131) << "lzma_easy_encoder returned " << result; |
| 98 |
} else |
| 99 |
kdWarning(7131) << "Unsupported mode " << mode << ". Only IO_ReadOnly and IO_WriteOnly supported"; |
| 100 |
d->mode = mode; |
| 101 |
d->isInitialized = true; |
| 102 |
} |
| 103 |
|
| 104 |
int KXzFilter::mode() const |
| 105 |
{ |
| 106 |
return d->mode; |
| 107 |
} |
| 108 |
|
| 109 |
void KXzFilter::terminate() |
| 110 |
{ |
| 111 |
if (d->mode == IO_ReadOnly || d->mode == IO_WriteOnly) { |
| 112 |
lzma_end(&d->zStream); |
| 113 |
} else { |
| 114 |
kdWarning(7131) << "Unsupported mode " << d->mode << ". Only IO_ReadOnly and IO_WriteOnly supported"; |
| 115 |
} |
| 116 |
d->isInitialized = false; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
void KXzFilter::reset() |
| 121 |
{ |
| 122 |
//kdDebug(7131) << "KXzFilter::reset"; |
| 123 |
// liblzma doesn't have a reset call... |
| 124 |
terminate(); |
| 125 |
init( d->mode ); |
| 126 |
} |
| 127 |
|
| 128 |
void KXzFilter::setOutBuffer( char * data, uint maxlen ) |
| 129 |
{ |
| 130 |
d->zStream.avail_out = maxlen; |
| 131 |
d->zStream.next_out = (uint8_t *)data; |
| 132 |
} |
| 133 |
|
| 134 |
void KXzFilter::setInBuffer( const char *data, unsigned int size ) |
| 135 |
{ |
| 136 |
d->zStream.avail_in = size; |
| 137 |
d->zStream.next_in = (uint8_t *)const_cast<char *>(data); |
| 138 |
} |
| 139 |
|
| 140 |
int KXzFilter::inBufferAvailable() const |
| 141 |
{ |
| 142 |
return d->zStream.avail_in; |
| 143 |
} |
| 144 |
|
| 145 |
int KXzFilter::outBufferAvailable() const |
| 146 |
{ |
| 147 |
return d->zStream.avail_out; |
| 148 |
} |
| 149 |
|
| 150 |
KXzFilter::Result KXzFilter::uncompress() |
| 151 |
{ |
| 152 |
//kdDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out =" << outBufferAvailable(); |
| 153 |
lzma_ret result = lzma_code(&d->zStream, LZMA_RUN); |
| 154 |
if ( result != LZMA_OK ) |
| 155 |
{ |
| 156 |
kdDebug(7131) << "lzma_code returned " << result; |
| 157 |
kdDebug(7131) << "KXzFilter::uncompress " << ( result == LZMA_STREAM_END ? END : ERROR ); |
| 158 |
} |
| 159 |
|
| 160 |
switch (result) { |
| 161 |
case LZMA_OK: |
| 162 |
return OK; |
| 163 |
case LZMA_STREAM_END: |
| 164 |
return END; |
| 165 |
default: |
| 166 |
return ERROR; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
KXzFilter::Result KXzFilter::compress( bool finish ) |
| 171 |
{ |
| 172 |
//kdDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable(); |
| 173 |
lzma_ret result = lzma_code(&d->zStream, finish ? LZMA_FINISH : LZMA_RUN ); |
| 174 |
|
| 175 |
switch (result) { |
| 176 |
case LZMA_OK: |
| 177 |
return OK; |
| 178 |
break; |
| 179 |
case LZMA_STREAM_END: |
| 180 |
kdDebug(7131) << " lzma_code returned " << result; |
| 181 |
return END; |
| 182 |
break; |
| 183 |
default: |
| 184 |
kdDebug(7131) << " lzma_code returned " << result; |
| 185 |
return ERROR; |
| 186 |
break; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
#endif /* HAVE_XZ_SUPPORT */ |