• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • compression
kxzfilter.cpp
Go to the documentation of this file.
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
25#include <config-compression.h>
26
27#if HAVE_XZ_SUPPORT
28extern "C" {
29 #include <lzma.h>
30}
31
32#include <QDebug>
33
34#include <qiodevice.h>
35
36
37class KXzFilter::Private
38{
39public:
40 Private()
41 : isInitialized(false)
42 {
43 memset(&zStream, 0, sizeof(zStream));
44 mode = 0;
45 }
46
47 lzma_stream zStream;
48 int mode;
49 bool isInitialized;
50};
51
52KXzFilter::KXzFilter()
53 :d(new Private)
54{
55}
56
57
58KXzFilter::~KXzFilter()
59{
60 delete d;
61}
62
63void KXzFilter::init( int mode )
64{
65 if (d->isInitialized) {
66 terminate();
67 }
68
69 lzma_ret result;
70 d->zStream.next_in = 0;
71 d->zStream.avail_in = 0;
72 if ( mode == QIODevice::ReadOnly )
73 {
74 /* We set the memlimit for decompression to 100MiB which should be
75 * more than enough to be sufficient for level 9 which requires 65 MiB.
76 */
77 result = lzma_auto_decoder(&d->zStream, 100<<20, 0);
78 //kDebug(7131) << "lzma_auto_decoder returned " << result;
79 } else if ( mode == QIODevice::WriteOnly ) {
80 result = lzma_easy_encoder(&d->zStream, LZMA_PRESET_DEFAULT, LZMA_CHECK_CRC32);
81 //kDebug(7131) << "lzma_easy_encoder returned " << result;
82 } else
83 qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
84 d->mode = mode;
85 d->isInitialized = true;
86}
87
88int KXzFilter::mode() const
89{
90 return d->mode;
91}
92
93void KXzFilter::terminate()
94{
95 if (d->mode == QIODevice::ReadOnly || d->mode == QIODevice::WriteOnly) {
96 lzma_end(&d->zStream);
97 } else {
98 qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
99 }
100 d->isInitialized = false;
101}
102
103
104void KXzFilter::reset()
105{
106 //kDebug(7131) << "KXzFilter::reset";
107 // liblzma doesn't have a reset call...
108 terminate();
109 init( d->mode );
110}
111
112void KXzFilter::setOutBuffer( char * data, uint maxlen )
113{
114 d->zStream.avail_out = maxlen;
115 d->zStream.next_out = (uint8_t *)data;
116}
117
118void KXzFilter::setInBuffer( const char *data, unsigned int size )
119{
120 d->zStream.avail_in = size;
121 d->zStream.next_in = (uint8_t *)const_cast<char *>(data);
122}
123
124int KXzFilter::inBufferAvailable() const
125{
126 return d->zStream.avail_in;
127}
128
129int KXzFilter::outBufferAvailable() const
130{
131 return d->zStream.avail_out;
132}
133
134KXzFilter::Result KXzFilter::uncompress()
135{
136 //kDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out =" << outBufferAvailable();
137 lzma_ret result = lzma_code(&d->zStream, LZMA_RUN);
138 if ( result != LZMA_OK )
139 {
140 qDebug() << "lzma_code returned " << result;
141 qDebug() << "KXzFilter::uncompress " << ( result == LZMA_STREAM_END ? KFilterBase::End : KFilterBase::Error );
142 }
143
144 switch (result) {
145 case LZMA_OK:
146 return KFilterBase::Ok;
147 case LZMA_STREAM_END:
148 return KFilterBase::End;
149 default:
150 return KFilterBase::Error;
151 }
152}
153
154KXzFilter::Result KXzFilter::compress( bool finish )
155{
156 //kDebug(7131) << "Calling lzma_code with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
157 lzma_ret result = lzma_code(&d->zStream, finish ? LZMA_FINISH : LZMA_RUN );
158
159 switch (result) {
160 case LZMA_OK:
161 return KFilterBase::Ok;
162 break;
163 case LZMA_STREAM_END:
164 qDebug() << " lzma_code returned " << result;
165 return KFilterBase::End;
166 break;
167 default:
168 qDebug() << " lzma_code returned " << result;
169 return KFilterBase::Error;
170 break;
171 }
172}
173
174#endif /* HAVE_XZ_SUPPORT */
KFilterBase::Error
@ Error
Definition: kfilterbase.h:82
KFilterBase::End
@ End
Definition: kfilterbase.h:82
KFilterBase::Ok
@ Ok
Definition: kfilterbase.h:82
kxzfilter.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal