summaryrefslogtreecommitdiffstats
path: root/kioslave/gzip
diff options
context:
space:
mode:
authortoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
committertoma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da>2009-11-25 17:56:58 +0000
commitce4a32fe52ef09d8f5ff1dd22c001110902b60a2 (patch)
tree5ac38a06f3dde268dc7927dc155896926aaf7012 /kioslave/gzip
downloadtdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.tar.gz
tdelibs-ce4a32fe52ef09d8f5ff1dd22c001110902b60a2.zip
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdelibs@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'kioslave/gzip')
-rw-r--r--kioslave/gzip/Makefile.am12
-rw-r--r--kioslave/gzip/kgzipfilter.cpp336
-rw-r--r--kioslave/gzip/kgzipfilter.desktop86
-rw-r--r--kioslave/gzip/kgzipfilter.h52
4 files changed, 486 insertions, 0 deletions
diff --git a/kioslave/gzip/Makefile.am b/kioslave/gzip/Makefile.am
new file mode 100644
index 000000000..2b290c206
--- /dev/null
+++ b/kioslave/gzip/Makefile.am
@@ -0,0 +1,12 @@
+INCLUDES = -I$(top_srcdir)/kio $(all_includes)
+AM_LDFLAGS = $(all_libraries) $(KDE_RPATH)
+METASOURCES = AUTO
+
+kde_module_LTLIBRARIES = kgzipfilter.la
+
+kgzipfilter_la_SOURCES = kgzipfilter.cpp
+kgzipfilter_la_LIBADD = $(LIB_KIO) $(LIBZ)
+kgzipfilter_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
+
+kde_services_DATA = kgzipfilter.desktop
+
diff --git a/kioslave/gzip/kgzipfilter.cpp b/kioslave/gzip/kgzipfilter.cpp
new file mode 100644
index 000000000..21380c6c1
--- /dev/null
+++ b/kioslave/gzip/kgzipfilter.cpp
@@ -0,0 +1,336 @@
+/* This file is part of the KDE libraries
+ Copyright (C) 2000 David Faure <faure@kde.org>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "kgzipfilter.h"
+#include <time.h>
+#include <zlib.h>
+#include <kdebug.h>
+#include <klibloader.h>
+
+/* gzip flag byte */
+#define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
+#define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
+#define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
+#define ORIG_NAME 0x08 /* bit 3 set: original file name present */
+#define COMMENT 0x10 /* bit 4 set: file comment present */
+#define RESERVED 0xE0 /* bits 5..7: reserved */
+
+
+// #define DEBUG_GZIP
+
+class KGzipFilterFactory : public KLibFactory
+{
+public:
+ KGzipFilterFactory() : KLibFactory() {}
+ ~KGzipFilterFactory(){}
+ QObject *createObject( QObject *parent, const char *name, const char*className, const QStringList & args )
+ {
+ Q_UNUSED(parent);
+ Q_UNUSED(name);
+ Q_UNUSED(className);
+ Q_UNUSED(args);
+ return new KGzipFilter;
+ }
+};
+
+K_EXPORT_COMPONENT_FACTORY( kgzipfilter, KGzipFilterFactory )
+
+// Not really necessary anymore, now that this is a dynamically-loaded lib.
+class KGzipFilter::KGzipFilterPrivate
+{
+public:
+ z_stream zStream;
+ bool bCompressed;
+};
+
+KGzipFilter::KGzipFilter()
+{
+ d = new KGzipFilterPrivate;
+ d->zStream.zalloc = (alloc_func)0;
+ d->zStream.zfree = (free_func)0;
+ d->zStream.opaque = (voidpf)0;
+}
+
+
+KGzipFilter::~KGzipFilter()
+{
+ delete d;
+}
+
+void KGzipFilter::init( int mode )
+{
+ d->zStream.next_in = Z_NULL;
+ d->zStream.avail_in = 0;
+ if ( mode == IO_ReadOnly )
+ {
+ int result = inflateInit2(&d->zStream, -MAX_WBITS); // windowBits is passed < 0 to suppress zlib header
+ if ( result != Z_OK )
+ kdDebug(7005) << "inflateInit returned " << result << endl;
+ // No idea what to do with result :)
+ } else if ( mode == IO_WriteOnly )
+ {
+ int result = deflateInit2(&d->zStream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY); // same here
+ if ( result != Z_OK )
+ kdDebug(7005) << "deflateInit returned " << result << endl;
+ } else {
+ kdWarning(7005) << "KGzipFilter: Unsupported mode " << mode << ". Only IO_ReadOnly and IO_WriteOnly supported" << endl;
+ }
+ m_mode = mode;
+ d->bCompressed = true;
+ m_headerWritten = false;
+}
+
+void KGzipFilter::terminate()
+{
+ if ( m_mode == IO_ReadOnly )
+ {
+ int result = inflateEnd(&d->zStream);
+ if ( result != Z_OK )
+ kdDebug(7005) << "inflateEnd returned " << result << endl;
+ } else if ( m_mode == IO_WriteOnly )
+ {
+ int result = deflateEnd(&d->zStream);
+ if ( result != Z_OK )
+ kdDebug(7005) << "deflateEnd returned " << result << endl;
+ }
+}
+
+
+void KGzipFilter::reset()
+{
+ if ( m_mode == IO_ReadOnly )
+ {
+ int result = inflateReset(&d->zStream);
+ if ( result != Z_OK )
+ kdDebug(7005) << "inflateReset returned " << result << endl;
+ } else if ( m_mode == IO_WriteOnly ) {
+ int result = deflateReset(&d->zStream);
+ if ( result != Z_OK )
+ kdDebug(7005) << "deflateReset returned " << result << endl;
+ m_headerWritten = false;
+ }
+}
+
+bool KGzipFilter::readHeader()
+{
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "KGzipFilter::readHeader avail=" << d->zStream.avail_in << endl;
+#endif
+ // Assume not compressed until we successfully decode the header
+ d->bCompressed = false;
+ // Assume the first block of data contains the whole header.
+ // The right way is to build this as a big state machine which
+ // is a pain in the ass.
+ // With 8K-blocks, we don't risk much anyway.
+ Bytef *p = d->zStream.next_in;
+ int i = d->zStream.avail_in;
+ if ((i -= 10) < 0) return false; // Need at least 10 bytes
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "KGzipFilter::readHeader first byte is " << QString::number(*p,16) << endl;
+#endif
+ if (*p++ != 0x1f) return false; // GZip magic
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "KGzipFilter::readHeader second byte is " << QString::number(*p,16) << endl;
+#endif
+ if (*p++ != 0x8b) return false;
+ int method = *p++;
+ int flags = *p++;
+ if ((method != Z_DEFLATED) || (flags & RESERVED) != 0) return false;
+ p += 6;
+ if ((flags & EXTRA_FIELD) != 0) // skip extra field
+ {
+ if ((i -= 2) < 0) return false; // Need at least 2 bytes
+ int len = *p++;
+ len += (*p++) << 8;
+ if ((i -= len) < 0) return false; // Need at least len bytes
+ p += len;
+ }
+ if ((flags & ORIG_NAME) != 0) // skip original file name
+ {
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "ORIG_NAME=" << p << endl;
+#endif
+ while( (i > 0) && (*p))
+ {
+ i--; p++;
+ }
+ if (--i <= 0) return false;
+ p++;
+ }
+ if ((flags & COMMENT) != 0) // skip comment
+ {
+ while( (i > 0) && (*p))
+ {
+ i--; p++;
+ }
+ if (--i <= 0) return false;
+ p++;
+ }
+ if ((flags & HEAD_CRC) != 0) // skip the header crc
+ {
+ if ((i-=2) < 0) return false;
+ p += 2;
+ }
+
+ d->zStream.avail_in = i;
+ d->zStream.next_in = p;
+ d->bCompressed = true;
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "header OK" << endl;
+#endif
+ return true;
+}
+
+/* Output a 16 bit value, lsb first */
+#define put_short(w) \
+ *p++ = (uchar) ((w) & 0xff); \
+ *p++ = (uchar) ((ushort)(w) >> 8);
+
+/* Output a 32 bit value to the bit stream, lsb first */
+#define put_long(n) \
+ put_short((n) & 0xffff); \
+ put_short(((ulong)(n)) >> 16);
+
+bool KGzipFilter::writeHeader( const QCString & fileName )
+{
+ Bytef *p = d->zStream.next_out;
+ int i = d->zStream.avail_out;
+ *p++ = 0x1f;
+ *p++ = 0x8b;
+ *p++ = Z_DEFLATED;
+ *p++ = ORIG_NAME;
+ put_long( time( 0L ) ); // Modification time (in unix format)
+ *p++ = 0; // Extra flags (2=max compress, 4=fastest compress)
+ *p++ = 3; // Unix
+
+ uint len = fileName.length();
+ for ( uint j = 0 ; j < len ; ++j )
+ *p++ = fileName[j];
+ *p++ = 0;
+ int headerSize = p - d->zStream.next_out;
+ i -= headerSize;
+ Q_ASSERT(i>0);
+ m_crc = crc32(0L, Z_NULL, 0);
+ d->zStream.next_out = p;
+ d->zStream.avail_out = i;
+ m_headerWritten = true;
+ return true;
+}
+
+void KGzipFilter::writeFooter()
+{
+ Q_ASSERT( m_headerWritten );
+ if (!m_headerWritten) kdDebug() << kdBacktrace();
+ Bytef *p = d->zStream.next_out;
+ int i = d->zStream.avail_out;
+ //kdDebug(7005) << "KGzipFilter::writeFooter writing CRC= " << QString::number( m_crc, 16 ) << endl;
+ put_long( m_crc );
+ //kdDebug(7005) << "KGzipFilter::writing writing totalin= " << d->zStream.total_in << endl;
+ put_long( d->zStream.total_in );
+ i -= p - d->zStream.next_out;
+ d->zStream.next_out = p;
+ d->zStream.avail_out = i;
+}
+
+void KGzipFilter::setOutBuffer( char * data, uint maxlen )
+{
+ d->zStream.avail_out = maxlen;
+ d->zStream.next_out = (Bytef *) data;
+}
+void KGzipFilter::setInBuffer( const char * data, uint size )
+{
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "KGzipFilter::setInBuffer avail_in=" << size << endl;
+#endif
+ d->zStream.avail_in = size;
+ d->zStream.next_in = (Bytef*) data;
+}
+int KGzipFilter::inBufferAvailable() const
+{
+ return d->zStream.avail_in;
+}
+int KGzipFilter::outBufferAvailable() const
+{
+ return d->zStream.avail_out;
+}
+
+KGzipFilter::Result KGzipFilter::uncompress_noop()
+{
+ // I'm not sure we really need support for that (uncompressed streams),
+ // but why not, it can't hurt to have it. One case I can think of is someone
+ // naming a tar file "blah.tar.gz" :-)
+ if ( d->zStream.avail_in > 0 )
+ {
+ int n = (d->zStream.avail_in < d->zStream.avail_out) ? d->zStream.avail_in : d->zStream.avail_out;
+ memcpy( d->zStream.next_out, d->zStream.next_in, n );
+ d->zStream.avail_out -= n;
+ d->zStream.next_in += n;
+ d->zStream.avail_in -= n;
+ return OK;
+ } else
+ return END;
+}
+
+KGzipFilter::Result KGzipFilter::uncompress()
+{
+ Q_ASSERT ( m_mode == IO_ReadOnly );
+ if ( d->bCompressed )
+ {
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << "Calling inflate with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
+ kdDebug(7005) << " next_in=" << d->zStream.next_in << endl;
+#endif
+ int result = inflate(&d->zStream, Z_SYNC_FLUSH);
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << " -> inflate returned " << result << endl;
+ kdDebug(7005) << "Now avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
+ kdDebug(7005) << " next_in=" << d->zStream.next_in << endl;
+#else
+ if ( result != Z_OK && result != Z_STREAM_END )
+ kdDebug(7005) << "Warning: inflate() returned " << result << endl;
+#endif
+ return ( result == Z_OK ? OK : ( result == Z_STREAM_END ? END : ERROR ) );
+ } else
+ return uncompress_noop();
+}
+
+KGzipFilter::Result KGzipFilter::compress( bool finish )
+{
+ Q_ASSERT ( d->bCompressed );
+ Q_ASSERT ( m_mode == IO_WriteOnly );
+
+ Bytef* p = d->zStream.next_in;
+ ulong len = d->zStream.avail_in;
+#ifdef DEBUG_GZIP
+ kdDebug(7005) << " calling deflate with avail_in=" << inBufferAvailable() << " avail_out=" << outBufferAvailable() << endl;
+#endif
+ int result = deflate(&d->zStream, finish ? Z_FINISH : Z_NO_FLUSH);
+ if ( result != Z_OK && result != Z_STREAM_END )
+ kdDebug(7005) << " deflate returned " << result << endl;
+ if ( m_headerWritten )
+ {
+ //kdDebug(7005) << "Computing CRC for the next " << len - d->zStream.avail_in << " bytes" << endl;
+ m_crc = crc32(m_crc, p, len - d->zStream.avail_in);
+ }
+ if ( result == Z_STREAM_END && m_headerWritten )
+ {
+ //kdDebug(7005) << "KGzipFilter::compress finished, write footer" << endl;
+ writeFooter();
+ }
+ return ( result == Z_OK ? OK : ( result == Z_STREAM_END ? END : ERROR ) );
+}
diff --git a/kioslave/gzip/kgzipfilter.desktop b/kioslave/gzip/kgzipfilter.desktop
new file mode 100644
index 000000000..e9f3c119c
--- /dev/null
+++ b/kioslave/gzip/kgzipfilter.desktop
@@ -0,0 +1,86 @@
+[Desktop Entry]
+Type=Service
+Name=GZip Filter
+Name[af]=Gzip Filter
+Name[ar]=فلتر GZip
+Name[az]=GZip Filtri
+Name[be]=Фільтр GZip
+Name[bg]=Филтър GZip
+Name[bn]=জি-জিপ (Gzip) ফিল্টার
+Name[br]=Sil GZip
+Name[ca]=Filtre GZip
+Name[cs]=Filtr GZip2
+Name[csb]=Filter GZipa
+Name[cy]=Hidl GZip
+Name[da]=GZip-filter
+Name[de]=GZip-Filter
+Name[el]=Φίλτρο GZip
+Name[eo]=GZip-filtrilo
+Name[es]=Filtro GZip
+Name[et]=GZip filter
+Name[eu]=GZip iragazkia
+Name[fa]=پالایۀ GZip
+Name[fi]=GZip-suodin
+Name[fr]=Filtre Gzip
+Name[fy]=GZip-filter
+Name[ga]=Scagaire gzip
+Name[gl]=Filtro GZip
+Name[he]=מסנן GZip
+Name[hi]=GZip फ़िल्टर
+Name[hr]=GZip filtar
+Name[hu]=GZip szűrő
+Name[id]=Filter Gzip
+Name[is]=GZip sía
+Name[it]=Filtro Gzip
+Name[ja]=GZip フィルタ
+Name[ka]=GZip ფილტრი
+Name[kk]=GZip сүзгісі
+Name[km]=តម្រង GZip
+Name[ko]=GZip 거르개
+Name[lb]=GZip-Filter
+Name[lt]=GZip filtras
+Name[lv]=GZip Filtrs
+Name[mk]=GZip филтер
+Name[mn]=GZip-Filter
+Name[ms]=Penapis GZip
+Name[mt]=Filtru GZip
+Name[nb]=GZip-filter
+Name[nds]=GZip-Filter
+Name[ne]=GZip फिल्टर
+Name[nl]=GZip-filter
+Name[nn]=GZip-filter
+Name[nso]=Sesekodi sa GZip
+Name[pa]=GZip ਫਿਲਟਰ
+Name[pl]=Filtr GZipa
+Name[pt]=Filtro GZip
+Name[pt_BR]=Filtro GZip
+Name[ro]=Filtru GZip
+Name[ru]=Фильтр gzip
+Name[rw]=Muyunguruzi GZipu
+Name[se]=GZip-filter
+Name[sk]=GZip filter
+Name[sl]=Filter za gzip
+Name[sq]=Filteri GZip
+Name[sr]=GZip филтер
+Name[sr@Latn]=GZip filter
+Name[ss]=Sisefo se GZip
+Name[sv]=Gzip-filter
+Name[ta]=GZip வடிகட்டி
+Name[te]=జిజిప్ గలని
+Name[tg]=Таровиши GZip
+Name[th]=ตัวกรอง GZip
+Name[tr]=GZip Filtresi
+Name[tt]=GZip Sözgeçe
+Name[uk]=Фільтр GZip
+Name[uz]=GZip-filter
+Name[uz@cyrillic]=GZip-филтер
+Name[ven]=Filithara ya GZip
+Name[vi]=Bộ lọc GZip
+Name[wa]=Passete GZip
+Name[xh]=Isihluzi se GZip
+Name[zh_CN]=GZip 过滤程序
+Name[zh_HK]=GZip 過濾器
+Name[zh_TW]=GZip 過濾器
+Name[zu]=Ihluzo le-GZip
+X-KDE-Library=kgzipfilter
+ServiceTypes=KDECompressionFilter,application/x-gzip,application/x-tgz
diff --git a/kioslave/gzip/kgzipfilter.h b/kioslave/gzip/kgzipfilter.h
new file mode 100644
index 000000000..c103d774d
--- /dev/null
+++ b/kioslave/gzip/kgzipfilter.h
@@ -0,0 +1,52 @@
+/* This file is part of the KDE libraries
+ Copyright (C) 2000 David Faure <faure@kde.org>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#ifndef __kgzipfilter__h
+#define __kgzipfilter__h
+
+#include "kfilterbase.h"
+
+class KGzipFilter : public KFilterBase
+{
+public:
+ KGzipFilter();
+ virtual ~KGzipFilter();
+
+ virtual void init( int mode );
+ virtual int mode() const { return m_mode; }
+ virtual void terminate();
+ virtual void reset();
+ virtual bool readHeader();
+ virtual bool writeHeader( const QCString & fileName );
+ void writeFooter();
+ virtual void setOutBuffer( char * data, uint maxlen );
+ virtual void setInBuffer( const char * data, uint size );
+ virtual int inBufferAvailable() const;
+ virtual int outBufferAvailable() const;
+ virtual Result uncompress();
+ virtual Result compress( bool finish );
+private:
+ Result uncompress_noop();
+ int m_mode;
+ ulong m_crc;
+ bool m_headerWritten;
+ class KGzipFilterPrivate;
+ KGzipFilterPrivate *d;
+};
+
+#endif