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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/* 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 "kfilterdev.h"
#include "kfilterbase.h"
#include <kdebug.h>
#include <stdio.h> // for EOF
#include <stdlib.h>
#include <assert.h>
#include <tqfile.h>
#define BUFFER_SIZE 8*1024
class KFilterDev::KFilterDevPrivate
{
public:
KFilterDevPrivate() : bNeedHeader(true), bSkipHeaders(false),
autoDeleteFilterBase(false), bOpenedUnderlyingDevice(false),
bIgnoreData(false){}
bool bNeedHeader;
bool bSkipHeaders;
bool autoDeleteFilterBase;
bool bOpenedUnderlyingDevice;
bool bIgnoreData;
TQByteArray buffer; // Used as 'input buffer' when reading, as 'output buffer' when writing
TQCString ungetchBuffer;
TQCString origFileName;
KFilterBase::Result result;
};
KFilterDev::KFilterDev( KFilterBase * _filter, bool autoDeleteFilterBase )
: filter(_filter)
{
assert(filter);
d = new KFilterDevPrivate;
d->autoDeleteFilterBase = autoDeleteFilterBase;
}
KFilterDev::~KFilterDev()
{
if ( isOpen() )
close();
if ( d->autoDeleteFilterBase )
delete filter;
delete d;
}
#ifndef KDE_NO_COMPAT
//this one is static
// Cumbersome API. To be removed in KDE 3.0.
TQIODevice* KFilterDev::createFilterDevice(KFilterBase* base, TQFile* file)
{
if (file==0)
return 0;
//we don't need a filter
if (base==0)
return TQT_TQIODEVICE(new TQFile(file->name())); // A bit strange IMHO. We ask for a TQFile but we create another one !?! (DF)
base->setDevice(TQT_TQIODEVICE(file));
return new KFilterDev(base);
}
#endif
//static
TQIODevice * KFilterDev::deviceForFile( const TQString & fileName, const TQString & mimetype,
bool forceFilter )
{
TQFile * f = new TQFile( fileName );
KFilterBase * base = mimetype.isEmpty() ? KFilterBase::findFilterByFileName( fileName )
: KFilterBase::findFilterByMimeType( mimetype );
if ( base )
{
base->setDevice(TQT_TQIODEVICE(f), true);
return new KFilterDev(base, true);
}
if(!forceFilter)
return TQT_TQIODEVICE(f);
else
{
delete f;
return 0L;
}
}
TQIODevice * KFilterDev::device( TQIODevice* inDevice, const TQString & mimetype)
{
return device( inDevice, mimetype, true );
}
TQIODevice * KFilterDev::device( TQIODevice* inDevice, const TQString & mimetype, bool autoDeleteInDevice )
{
if (inDevice==0)
return 0;
KFilterBase * base = KFilterBase::findFilterByMimeType(mimetype);
if ( base )
{
base->setDevice(inDevice, autoDeleteInDevice);
return new KFilterDev(base, true /* auto-delete "base" */);
}
return 0;
}
bool KFilterDev::open( TQ_OpenMode mode )
{
//kdDebug(7005) << "KFilterDev::open " << mode << endl;
if ( mode == IO_ReadOnly )
{
d->buffer.resize(0);
d->ungetchBuffer.resize(0);
}
else
{
d->buffer.resize( BUFFER_SIZE );
filter->setOutBuffer( d->buffer.data(), d->buffer.size() );
}
d->bNeedHeader = !d->bSkipHeaders;
filter->init( mode );
d->bOpenedUnderlyingDevice = !filter->device()->isOpen();
bool ret = d->bOpenedUnderlyingDevice ? filter->device()->open( (TQ_OpenMode)mode ) : true;
d->result = KFilterBase::OK;
if ( !ret )
kdWarning(7005) << "KFilterDev::open: Couldn't open underlying device" << endl;
else
{
setState( IO_Open );
setMode( mode );
}
TQIODevice::at(0);
return ret;
}
void KFilterDev::close()
{
if ( !isOpen() )
return;
//kdDebug(7005) << "KFilterDev::close" << endl;
if ( filter->mode() == IO_WriteOnly )
writeBlock( 0L, 0 ); // finish writing
//kdDebug(7005) << "KFilterDev::close. Calling terminate()." << endl;
filter->terminate();
if ( d->bOpenedUnderlyingDevice )
filter->device()->close();
setState( 0 ); // not IO_Open
}
void KFilterDev::flush()
{
//kdDebug(7005) << "KFilterDev::flush" << endl;
filter->device()->flush();
// Hmm, might not be enough...
}
#ifdef USE_QT4
qint64 KFilterDev::size() const
#else // USE_QT4
TQIODevice::Offset KFilterDev::size() const
#endif // USE_QT4
{
// Well, hmm, Houston, we have a problem.
// We can't know the size of the uncompressed data
// before uncompressing it.......
// But readAll, which is not virtual, needs the size.........
kdWarning(7005) << "KFilterDev::size - can't be implemented !!!!!!!! Returning -1 " << endl;
//abort();
return (uint)-1;
}
TQIODevice::Offset KFilterDev::at() const
{
return TQIODevice::at();
}
bool KFilterDev::at( TQIODevice::Offset pos )
{
//kdDebug(7005) << "KFilterDev::at " << pos << " currently at " << TQIODevice::at() << endl;
if ( TQIODevice::at() == pos )
return true;
Q_ASSERT ( filter->mode() == IO_ReadOnly );
if ( pos == 0 )
{
TQIODevice::at(0);
// We can forget about the cached data
d->ungetchBuffer.resize(0);
d->bNeedHeader = !d->bSkipHeaders;
d->result = KFilterBase::OK;
filter->setInBuffer(0L,0);
filter->reset();
return filter->device()->reset();
}
if ( TQIODevice::at() < pos ) // we can start from here
pos = pos - TQIODevice::at();
else
{
// we have to start from 0 ! Ugly and slow, but better than the previous
// solution (KTarGz was allocating everything into memory)
if (!at(0)) // sets ioIndex to 0
return false;
}
//kdDebug(7005) << "KFilterDev::at : reading " << pos << " dummy bytes" << endl;
TQByteArray dummy( TQMIN( pos, 3*BUFFER_SIZE ) );
d->bIgnoreData = true;
bool result = ( (TQIODevice::Offset)readBlock( dummy.data(), pos ) == pos );
d->bIgnoreData = false;
return result;
}
bool KFilterDev::atEnd() const
{
return filter->device()->atEnd() && (d->result == KFilterBase::END)
&& d->ungetchBuffer.isEmpty();
}
TQT_TQIO_LONG KFilterDev::tqreadBlock( char *data, TQT_TQIO_ULONG maxlen )
{
Q_ASSERT ( filter->mode() == IO_ReadOnly );
//kdDebug(7005) << "KFilterDev::readBlock maxlen=" << maxlen << endl;
uint dataReceived = 0;
if ( !d->ungetchBuffer.isEmpty() )
{
uint len = d->ungetchBuffer.length();
if ( !d->bIgnoreData )
{
while ( ( dataReceived < len ) && ( dataReceived < maxlen ) )
{
*data = d->ungetchBuffer[ len - dataReceived - 1 ];
data++;
dataReceived++;
}
}
else
{
dataReceived = TQMIN( len, maxlen );
}
d->ungetchBuffer.truncate( len - dataReceived );
TQIODevice::at(TQIODevice::at() + dataReceived);
}
// If we came to the end of the stream
// return what we got from the ungetchBuffer.
if ( d->result == KFilterBase::END )
return dataReceived;
// If we had an error, return -1.
if ( d->result != KFilterBase::OK )
return -1;
TQ_ULONG outBufferSize;
if ( d->bIgnoreData )
{
outBufferSize = TQMIN( maxlen, 3*BUFFER_SIZE );
}
else
{
outBufferSize = maxlen;
}
outBufferSize -= dataReceived;
TQ_ULONG availOut = outBufferSize;
filter->setOutBuffer( data, outBufferSize );
bool decompressedAll = false;
while ( dataReceived < maxlen )
{
if (filter->inBufferEmpty())
{
// Not sure about the best size to set there.
// For sure, it should be bigger than the header size (see comment in readHeader)
d->buffer.resize( BUFFER_SIZE );
// Request data from underlying device
int size = filter->device()->readBlock( d->buffer.data(),
d->buffer.size() );
if ( size )
filter->setInBuffer( d->buffer.data(), size );
else {
if ( decompressedAll )
{
// We decoded everything there was to decode. So -> done.
//kdDebug(7005) << "Seems we're done. dataReceived=" << dataReceived << endl;
d->result = KFilterBase::END;
break;
}
}
//kdDebug(7005) << "KFilterDev::readBlock got " << size << " bytes from device" << endl;
}
if (d->bNeedHeader)
{
(void) filter->readHeader();
d->bNeedHeader = false;
}
d->result = filter->uncompress();
if (d->result == KFilterBase::ERROR)
{
kdWarning(7005) << "KFilterDev: Error when uncompressing data" << endl;
break;
}
// We got that much data since the last time we went here
uint outReceived = availOut - filter->outBufferAvailable();
//kdDebug(7005) << "avail_out = " << filter->outBufferAvailable() << " result=" << d->result << " outReceived=" << outReceived << endl;
if( availOut < (uint)filter->outBufferAvailable() )
kdWarning(7005) << " last availOut " << availOut << " smaller than new avail_out=" << filter->outBufferAvailable() << " !" << endl;
dataReceived += outReceived;
if ( !d->bIgnoreData ) // Move on in the output buffer
{
data += outReceived;
availOut = maxlen - dataReceived;
}
else if ( maxlen - dataReceived < outBufferSize )
{
availOut = maxlen - dataReceived;
}
TQIODevice::at(TQIODevice::at() + outReceived);
if (d->result == KFilterBase::END)
{
//kdDebug(7005) << "KFilterDev::readBlock got END. dataReceived=" << dataReceived << endl;
break; // Finished.
}
if (filter->inBufferEmpty() && filter->outBufferAvailable() != 0 )
{
decompressedAll = true;
}
filter->setOutBuffer( data, availOut );
}
return dataReceived;
}
TQT_TQIO_LONG KFilterDev::tqwriteBlock( const char *data /*0 to finish*/, TQT_TQIO_ULONG len )
{
Q_ASSERT ( filter->mode() == IO_WriteOnly );
// If we had an error, return 0.
if ( d->result != KFilterBase::OK )
return 0;
bool finish = (data == 0L);
if (!finish)
{
filter->setInBuffer( data, len );
if (d->bNeedHeader)
{
(void)filter->writeHeader( d->origFileName );
d->bNeedHeader = false;
}
}
uint dataWritten = 0;
uint availIn = len;
while ( dataWritten < len || finish )
{
d->result = filter->compress( finish );
if (d->result == KFilterBase::ERROR)
{
kdWarning(7005) << "KFilterDev: Error when compressing data" << endl;
// What to do ?
break;
}
// Wrote everything ?
if (filter->inBufferEmpty() || (d->result == KFilterBase::END))
{
// We got that much data since the last time we went here
uint wrote = availIn - filter->inBufferAvailable();
//kdDebug(7005) << " Wrote everything for now. avail_in = " << filter->inBufferAvailable() << " result=" << d->result << " wrote=" << wrote << endl;
// Move on in the input buffer
data += wrote;
dataWritten += wrote;
TQIODevice::at(TQIODevice::at() + wrote);
availIn = len - dataWritten;
//kdDebug(7005) << " KFilterDev::writeBlock availIn=" << availIn << " dataWritten=" << dataWritten << " ioIndex=" << ioIndex << endl;
if ( availIn > 0 ) // Not sure this will ever happen
filter->setInBuffer( data, availIn );
}
if (filter->outBufferFull() || (d->result == KFilterBase::END))
{
//kdDebug(7005) << " KFilterDev::writeBlock writing to underlying. avail_out=" << filter->outBufferAvailable() << endl;
int towrite = d->buffer.size() - filter->outBufferAvailable();
if ( towrite > 0 )
{
// Write compressed data to underlying device
int size = filter->device()->writeBlock( d->buffer.data(), towrite );
if ( size != towrite ) {
kdWarning(7005) << "KFilterDev::writeBlock. Could only write " << size << " out of " << towrite << " bytes" << endl;
return 0; // indicate an error (happens on disk full)
}
//else
//kdDebug(7005) << " KFilterDev::writeBlock wrote " << size << " bytes" << endl;
}
d->buffer.resize( 8*1024 );
filter->setOutBuffer( d->buffer.data(), d->buffer.size() );
if (d->result == KFilterBase::END)
{
//kdDebug(7005) << " KFilterDev::writeBlock END" << endl;
Q_ASSERT(finish); // hopefully we don't get end before finishing
break;
}
}
}
return dataWritten;
}
int KFilterDev::getch()
{
Q_ASSERT ( filter->mode() == IO_ReadOnly );
//kdDebug(7005) << "KFilterDev::getch" << endl;
if ( !d->ungetchBuffer.isEmpty() ) {
int len = d->ungetchBuffer.length();
int ch = d->ungetchBuffer[ len-1 ];
d->ungetchBuffer.truncate( len - 1 );
TQIODevice::at(TQIODevice::at() + 1);
//kdDebug(7005) << "KFilterDev::getch from ungetch: " << TQString(TQChar(ch)) << endl;
return ch;
}
char buf[1];
int ret = readBlock( buf, 1 ) == 1 ? buf[0] : EOF;
//kdDebug(7005) << "KFilterDev::getch ret=" << TQString(TQChar(ret)) << endl;
return ret;
}
int KFilterDev::putch( int c )
{
//kdDebug(7005) << "KFilterDev::putch" << endl;
char buf[1];
buf[0] = c;
return writeBlock( buf, 1 ) == 1 ? c : -1;
}
int KFilterDev::ungetch( int ch )
{
//kdDebug(7005) << "KFilterDev::ungetch " << TQString(TQChar(ch)) << endl;
if ( ch == EOF ) // cannot unget EOF
return ch;
// pipe or similar => we cannot ungetch, so do it manually
d->ungetchBuffer +=ch;
TQIODevice::at(TQIODevice::at() - 1);
return ch;
}
void KFilterDev::setOrigFileName( const TQCString & fileName )
{
d->origFileName = fileName;
}
void KFilterDev::setSkipHeaders()
{
d->bSkipHeaders = true;
}
|