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

KDECore

  • kdecore
  • compression
kbzip2filter.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2000-2005 David Faure <faure@kde.org>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kbzip2filter.h"
21 
22 #include <config-compression.h>
23 
24 #if HAVE_BZIP2_SUPPORT
25 
26 // we don't need that
27 #define BZ_NO_STDIO
28 extern "C" {
29  #include <bzlib.h>
30 }
31 
32 #if NEED_BZ2_PREFIX
33  #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z)
34  #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x)
35  #define bzCompressEnd(x) BZ2_bzCompressEnd(x)
36  #define bzDecompress(x) BZ2_bzDecompress(x)
37  #define bzCompress(x,y) BZ2_bzCompress(x, y)
38  #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a);
39 #endif
40 
41 #include <kdebug.h>
42 
43 #include <QDebug>
44 
45 #include <qiodevice.h>
46 
47 
48 
49 // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html
50 
51 class KBzip2Filter::Private
52 {
53 public:
54  Private()
55  : isInitialized(false)
56  {
57  memset(&zStream, 0, sizeof(zStream));
58  mode = 0;
59  }
60 
61  bz_stream zStream;
62  int mode;
63  bool isInitialized;
64 };
65 
66 KBzip2Filter::KBzip2Filter()
67  :d(new Private)
68 {
69 }
70 
71 
72 KBzip2Filter::~KBzip2Filter()
73 {
74  delete d;
75 }
76 
77 void KBzip2Filter::init( int mode )
78 {
79  if (d->isInitialized) {
80  terminate();
81  }
82 
83  d->zStream.next_in = 0;
84  d->zStream.avail_in = 0;
85  if ( mode == QIODevice::ReadOnly )
86  {
87  (void)bzDecompressInit(&d->zStream, 0, 0);
88  //qDebug() << "bzDecompressInit returned " << result;
89  // TODO: test result and return false on error
90  } else if ( mode == QIODevice::WriteOnly ) {
91  (void)bzCompressInit(&d->zStream, 5, 0, 0);
92  //qDebug() << "bzDecompressInit returned " << result;
93  // TODO: test result and return false on error
94  } else {
95  qWarning() << "Unsupported mode " << mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
96  // TODO return false
97  }
98  d->mode = mode;
99  d->isInitialized = true;
100 }
101 
102 int KBzip2Filter::mode() const
103 {
104  return d->mode;
105 }
106 
107 void KBzip2Filter::terminate()
108 {
109  if (d->mode == QIODevice::ReadOnly) {
110  int result = bzDecompressEnd(&d->zStream);
111  // TODO: test result and return false on error
112  //qDebug() << "bzDecompressEnd returned " << result;
113  } else if (d->mode == QIODevice::WriteOnly) {
114  int result = bzCompressEnd(&d->zStream);
115  // TODO: test result and return false on error
116  //qDebug() << "bzCompressEnd returned " << result;
117  } else {
118  qWarning() << "Unsupported mode " << d->mode << ". Only QIODevice::ReadOnly and QIODevice::WriteOnly supported";
119  // TODO return false
120  }
121  d->isInitialized = false;
122 }
123 
124 
125 void KBzip2Filter::reset()
126 {
127  // bzip2 doesn't seem to have a reset call...
128  terminate();
129  init( d->mode );
130 }
131 
132 void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
133 {
134  d->zStream.avail_out = maxlen;
135  d->zStream.next_out = data;
136 }
137 
138 void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
139 {
140  d->zStream.avail_in = size;
141  d->zStream.next_in = const_cast<char *>(data);
142 }
143 
144 int KBzip2Filter::inBufferAvailable() const
145 {
146  return d->zStream.avail_in;
147 }
148 
149 int KBzip2Filter::outBufferAvailable() const
150 {
151  return d->zStream.avail_out;
152 }
153 
154 KBzip2Filter::Result KBzip2Filter::uncompress()
155 {
156  //qDebug() << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
157  int result = bzDecompress(&d->zStream);
158  if ( result < BZ_OK )
159  {
160  kWarning() << "bzDecompress returned" << result;
161  }
162 
163  switch (result) {
164  case BZ_OK:
165  return KFilterBase::Ok;
166  case BZ_STREAM_END:
167  return KFilterBase::End;
168  default:
169  return KFilterBase::Error;
170  }
171 }
172 
173 KBzip2Filter::Result KBzip2Filter::compress( bool finish )
174 {
175  //qDebug() << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable();
176  int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
177 
178  switch (result) {
179  case BZ_OK:
180  case BZ_FLUSH_OK:
181  case BZ_RUN_OK:
182  case BZ_FINISH_OK:
183  return KFilterBase::Ok;
184  break;
185  case BZ_STREAM_END:
186  //qDebug() << " bzCompress returned " << result;
187  return KFilterBase::End;
188  break;
189  default:
190  //qDebug() << " bzCompress returned " << result;
191  return KFilterBase::Error;
192  break;
193  }
194 }
195 
196 #endif /* HAVE_BZIP2_SUPPORT */
KFilterBase::Error
@ Error
Definition: kfilterbase.h:82
KFilterBase::End
@ End
Definition: kfilterbase.h:82
KFilterBase::Ok
@ Ok
Definition: kfilterbase.h:82
kWarning
#define kWarning
Definition: kdebug.h:322
kbzip2filter.h
kdebug.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Feb 3 2022 00:00:00 by doxygen 1.9.1 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