TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kbzip2filter.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* This file is part of the KDE libraries
  2. Copyright (C) 2000 David Faure <faure@kde.org>
  3. $Id$
  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 version 2 as published by the Free Software Foundation.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include <config.h>
  17. #if defined( HAVE_BZIP2_SUPPORT )
  18. // we don't need that
  19. #define BZ_NO_STDIO
  20. extern "C" {
  21. #include <bzlib.h>
  22. }
  23. #ifdef NEED_BZ2_PREFIX
  24. #define bzDecompressInit(x,y,z) BZ2_bzDecompressInit(x,y,z)
  25. #define bzDecompressEnd(x) BZ2_bzDecompressEnd(x)
  26. #define bzCompressEnd(x) BZ2_bzCompressEnd(x)
  27. #define bzDecompress(x) BZ2_bzDecompress(x)
  28. #define bzCompress(x,y) BZ2_bzCompress(x, y)
  29. #define bzCompressInit(x,y,z,a) BZ2_bzCompressInit(x, y, z, a);
  30. #endif
  31. #include <kdebug.h>
  32. #include <klibloader.h>
  33. #include "kbzip2filter.h"
  34. // For docu on this, see /usr/doc/bzip2-0.9.5d/bzip2-0.9.5d/manual_3.html
  35. class KBzip2FilterFactory : public KLibFactory
  36. {
  37. public:
  38. KBzip2FilterFactory() : KLibFactory() {}
  39. virtual ~KBzip2FilterFactory(){}
  40. TQObject *createObject( TQObject *, const char *, const char*, const TQStringList & )
  41. {
  42. return new KBzip2Filter;
  43. }
  44. };
  45. K_EXPORT_COMPONENT_FACTORY( kbzip2filter, KBzip2FilterFactory )
  46. // Not really useful anymore
  47. class KBzip2Filter::KBzip2FilterPrivate
  48. {
  49. public:
  50. bz_stream zStream;
  51. };
  52. KBzip2Filter::KBzip2Filter()
  53. {
  54. d = new KBzip2FilterPrivate;
  55. d->zStream.bzalloc = 0;
  56. d->zStream.bzfree = 0;
  57. d->zStream.opaque = 0;
  58. m_mode = 0;
  59. }
  60. KBzip2Filter::~KBzip2Filter()
  61. {
  62. delete d;
  63. }
  64. void KBzip2Filter::init( int mode )
  65. {
  66. d->zStream.next_in = 0;
  67. d->zStream.avail_in = 0;
  68. if ( mode == IO_ReadOnly )
  69. {
  70. (void)bzDecompressInit(&d->zStream, 0, 0);
  71. //kdDebug(7118) << "bzDecompressInit returned " << result << endl;
  72. // No idea what to do with result :)
  73. } else if ( mode == IO_WriteOnly ) {
  74. (void)bzCompressInit(&d->zStream, 5, 0, 0);
  75. //kdDebug(7118) << "bzDecompressInit returned " << result << endl;
  76. } else
  77. kdWarning(7118) << "Unsupported mode " << mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
  78. m_mode = mode;
  79. }
  80. void KBzip2Filter::terminate()
  81. {
  82. if ( m_mode == IO_ReadOnly )
  83. {
  84. int result = bzDecompressEnd(&d->zStream);
  85. kdDebug(7118) << "bzDecompressEnd returned " << result << endl;
  86. } else if ( m_mode == IO_WriteOnly )
  87. {
  88. int result = bzCompressEnd(&d->zStream);
  89. kdDebug(7118) << "bzCompressEnd returned " << result << endl;
  90. } else
  91. kdWarning(7118) << "Unsupported mode " << m_mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
  92. }
  93. void KBzip2Filter::reset()
  94. {
  95. kdDebug(7118) << "KBzip2Filter::reset" << endl;
  96. // bzip2 doesn't seem to have a reset call...
  97. terminate();
  98. init( m_mode );
  99. }
  100. void KBzip2Filter::setOutBuffer( char * data, uint maxlen )
  101. {
  102. d->zStream.avail_out = maxlen;
  103. d->zStream.next_out = data;
  104. }
  105. void KBzip2Filter::setInBuffer( const char *data, unsigned int size )
  106. {
  107. d->zStream.avail_in = size;
  108. d->zStream.next_in = const_cast<char *>(data);
  109. }
  110. int KBzip2Filter::inBufferAvailable() const
  111. {
  112. return d->zStream.avail_in;
  113. }
  114. int KBzip2Filter::outBufferAvailable() const
  115. {
  116. return d->zStream.avail_out;
  117. }
  118. KBzip2Filter::Result KBzip2Filter::uncompress()
  119. {
  120. //kdDebug(7118) << "Calling bzDecompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
  121. int result = bzDecompress(&d->zStream);
  122. if ( result != BZ_OK )
  123. {
  124. kdDebug(7118) << "bzDecompress returned " << result << endl;
  125. kdDebug(7118) << "KBzip2Filter::uncompress " << ( result == BZ_OK ? OK : ( result == BZ_STREAM_END ? END : ERROR ) ) << endl;
  126. }
  127. switch (result) {
  128. case BZ_OK:
  129. return OK;
  130. case BZ_STREAM_END:
  131. return END;
  132. default:
  133. return ERROR;
  134. }
  135. }
  136. KBzip2Filter::Result KBzip2Filter::compress( bool finish )
  137. {
  138. //kdDebug(7118) << "Calling bzCompress with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
  139. int result = bzCompress(&d->zStream, finish ? BZ_FINISH : BZ_RUN );
  140. switch (result) {
  141. case BZ_OK:
  142. case BZ_FLUSH_OK:
  143. case BZ_RUN_OK:
  144. case BZ_FINISH_OK:
  145. return OK;
  146. break;
  147. case BZ_STREAM_END:
  148. kdDebug(7118) << " bzCompress returned " << result << endl;
  149. return END;
  150. break;
  151. default:
  152. kdDebug(7118) << " bzCompress returned " << result << endl;
  153. return ERROR;
  154. break;
  155. }
  156. }
  157. #endif