summaryrefslogtreecommitdiffstats
path: root/tdeioslave/gzip/kgzipfilter.cpp
blob: 375f9f9bc126129afa2b16a776e1ac05d8f4a09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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(){}
    TQObject *createObject( TQObject *parent, const char *name, const char*className, const TQStringList & 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 " << TQString::number(*p,16) << endl;
#endif
    if (*p++ != 0x1f) return false; // GZip magic
#ifdef DEBUG_GZIP
    kdDebug(7005) << "KGzipFilter::readHeader second byte is " << TQString::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 TQCString & 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= " << TQString::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 ) );
}