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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
|
//=============================================================================
//
// File : kvi_packagefile.cpp
// Created on Tue 26 Dec 2006 05:33:33 by Szymon Stefanek
//
// This file is part of the KVIrc IRC Client distribution
// Copyright (C) 2006 Szymon Stefanek <pragma at kvirc dot net>
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#define __KVILIB__
#include "kvi_packagefile.h"
#include "kvi_file.h"
#include "kvi_fileutils.h"
#include "kvi_locale.h"
#include "kvi_inttypes.h"
#include <qprogressdialog.h>
#include <qlabel.h>
#include <qdir.h>
#ifdef COMPILE_ZLIB_SUPPORT
#include <zlib.h>
#endif
//
// A KVIrc Package File is basically a simple zip file with some additional meta-data.
// The package file has the following format
//
// Field Type Bytes Description
//-------------------------------------------------------------------------------
// Package:
// PackageHeader
// PackageInfo
// PackageData
// PackageHeader:
// Magic Bytes 4 'KVPF': Signature for the Kvirc Package File
// Version uint32 4 0x00000001: Version of this package file
// Flags uint32 4 0x00000000: Flags, in version 1 is reserved and must be zero
//
// PackageInfo:
// InfoFieldCount uint32 4 Number of package info fields
// InfoField InfoField Variable A list of informational name-value pairs
// InfoField InfoField Variable A list of informational name-value pairs
// InfoField InfoField Variable A list of informational name-value pairs
// .... .... ....
// PackageData:
// DataField DataField Variable A list of data fields with format defined below
// DataField DataField Variable A list of data fields with format defined below
// DataField DataField Variable A list of data fields with format defined below
// .... .... ....
// InfoField:
// Name UniString Variable The "name" element of the info field
// ValueType uint32 4 The type of the following ValueData field
// ValueData ValueData Variable
// ValueData for ValueType 1 (string field)
// Value UniString Variable The value element of type string of the the info field
// ValueData for ValueType 2 (binary buffer field)
// BufferLen uint32 4 The length of the binary buffer
// BufferData Bytes Variable The data for the binary buffer
// UniString:
// StringLen uint32 4 The length of the string data in BYTES (null terminator NOT included)
// StringData Bytes StringLen An utf8 encoded string (do NOT write the NULL terminator)
// Bytes:
// Byte uint8 1 A byte
// Byte uint8 1 A byte
// .... .... ....
// DataField:
// FieldType uint32 4 The type of the field, see below for defined values
// FieldLen uint32 4 FieldData length in bytes (useful for skipping a field if unsupported)
// FieldData Variable FieldLen The data of the field, see below for defined values
// FieldData for FieldType 1 (file field)
// Flags uint32 4 Bitmask. Bits: 1=FileIsDeflated
// Path UniString Variable A relative path expressed as utf8 string. \ AND / are considered to be separators
// Size uint32 4 Size of the following file data
// FilePayload Bytes Variable
// Everything is stored in LITTLE ENDIAN byte order.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Da Base Engine
KviPackageIOEngine::KviPackageIOEngine()
{
m_pProgressDialog = 0;
m_pStringInfoFields = new KviPointerHashTable<QString,QString>();
m_pStringInfoFields->setAutoDelete(true);
m_pBinaryInfoFields = new KviPointerHashTable<QString,QByteArray>();
m_pBinaryInfoFields->setAutoDelete(true);
}
KviPackageIOEngine::~KviPackageIOEngine()
{
if(m_pProgressDialog)delete m_pProgressDialog;
delete m_pStringInfoFields;
delete m_pBinaryInfoFields;
}
bool KviPackageIOEngine::updateProgress(int iProgress,const QString &szLabel)
{
if(!m_pProgressDialog)return true;
#ifdef COMPILE_USE_QT4
m_pProgressDialog->setValue(iProgress);
#else
m_pProgressDialog->setProgress(iProgress);
#endif
m_pProgressDialogLabel->setText(szLabel);
qApp->processEvents();
if(m_pProgressDialog->wasCanceled())
{
setLastError(__tr2qs("Operation cancelled"));
return false;
}
return true;
}
void KviPackageIOEngine::showProgressDialog(const QString &szCaption,int iTotalSteps)
{
#ifdef COMPILE_USE_QT4
m_pProgressDialog = new QProgressDialog(QString(""),__tr2qs("Cancel"),0,iTotalSteps,0);
m_pProgressDialog->setModal(true);
m_pProgressDialog->setWindowTitle(szCaption);
#else
m_pProgressDialog = new QProgressDialog(QString(""),__tr2qs("Cancel"),iTotalSteps,0,"",true);
m_pProgressDialog->setCaption(szCaption);
#endif
m_pProgressDialogLabel = new QLabel(m_pProgressDialog);
m_pProgressDialogLabel->setMaximumSize(500,300);
m_pProgressDialog->setLabel(m_pProgressDialogLabel);
}
void KviPackageIOEngine::hideProgressDialog()
{
if(!m_pProgressDialog)return;
delete m_pProgressDialog;
m_pProgressDialog = 0;
}
bool KviPackageIOEngine::writeError()
{
setLastError(__tr2qs("File write error"));
return false;
}
bool KviPackageIOEngine::readError()
{
setLastError(__tr2qs("File read error"));
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Da Writer
KviPackageWriter::KviPackageWriter()
: KviPackageIOEngine()
{
m_pDataFields = new KviPointerList<DataField>();
m_pDataFields->setAutoDelete(true);
}
KviPackageWriter::~KviPackageWriter()
{
delete m_pDataFields;
}
void KviPackageWriter::addInfoField(const QString &szName,const QString &szValue)
{
m_pStringInfoFields->replace(szName,new QString(szValue));
}
void KviPackageWriter::addInfoField(const QString &szName,QByteArray * pValue)
{
m_pBinaryInfoFields->replace(szName,pValue);
}
bool KviPackageWriter::addFile(const QString &szLocalFileName,const QString &szTargetFileName,kvi_u32_t uAddFileFlags)
{
QFileInfo fi(szLocalFileName);
return addFileInternal(&fi,szLocalFileName,szTargetFileName,uAddFileFlags);
}
bool KviPackageWriter::addFileInternal(const QFileInfo * fi,const QString &szLocalFileName,const QString &szTargetFileName,kvi_u32_t uAddFileFlags)
{
if(!(fi->isFile() && fi->isReadable()))
return false;
if(!(uAddFileFlags & FollowSymLinks))
{
if(fi->isSymLink())
return true; // do NOT add a symlink
}
DataField * f = new DataField();
f->m_uType = KVI_PACKAGE_DATAFIELD_TYPE_FILE;
f->m_bFileAllowCompression = !(uAddFileFlags & NoCompression);
f->m_szFileLocalName = szLocalFileName;
f->m_szFileTargetName = szTargetFileName;
m_pDataFields->append(f);
return true;
}
bool KviPackageWriter::addDirectory(const QString &szLocalDirectoryName,const QString &szTargetDirectoryPrefix,kvi_u32_t uAddFileFlags)
{
QDir d(szLocalDirectoryName);
#ifdef COMPILE_USE_QT4
QDir::Filters iFlags;
#else
int iFlags;
#endif
iFlags = QDir::Files | QDir::Readable;
if(!(uAddFileFlags & FollowSymLinks))
iFlags |= QDir::NoSymLinks;
// QT4SUX: Because the QDir::entryInfoList() breaks really a lot of code by returning an object that behaves in a _totally_ different way.. it's also much slower
#ifdef COMPILE_USE_QT4
int j;
QFileInfoList sl = d.entryInfoList(iFlags);
for(j=0;j<sl.size();j++)
{
#else
const QFileInfoList * sl = d.entryInfoList(iFlags);
if(!sl)return false;
QFileInfoListIterator it(*sl);
while(QFileInfo * fi = it.current())
{
#endif
QString szSFileName = szLocalDirectoryName;
KviQString::ensureLastCharIs(szSFileName,QChar(KVI_PATH_SEPARATOR_CHAR));
#ifdef COMPILE_USE_QT4
QFileInfo slowCopy = sl.at(j);
szSFileName += slowCopy.fileName();
#else
szSFileName += fi->fileName();
#endif
QString szDFileName = szTargetDirectoryPrefix;
KviQString::ensureLastCharIs(szDFileName,QChar(KVI_PATH_SEPARATOR_CHAR));
#ifdef COMPILE_USE_QT4
szDFileName += slowCopy.fileName();
if(!addFileInternal(&slowCopy,szSFileName,szDFileName,uAddFileFlags))
return false;
#else
szDFileName += fi->fileName();
if(!addFileInternal(fi,szSFileName,szDFileName,uAddFileFlags))
return false;
#endif
#ifndef COMPILE_USE_QT4
++it;
#endif
}
iFlags = QDir::Dirs | QDir::Readable;
if(!(uAddFileFlags & FollowSymLinks))
iFlags |= QDir::NoSymLinks;
sl = d.entryInfoList(iFlags);
#ifdef COMPILE_USE_QT4
for(j=0;j<sl.size();j++)
{
QString szDir = sl.at(j).fileName();
#else
if(!sl)return false;
QFileInfoListIterator it2(*sl);
while(QFileInfo * fi2 = it2.current())
{
QString szDir = fi2->fileName();
#endif
if(!KviQString::equalCS(szDir,"..") && !KviQString::equalCS(szDir,"."))
{
QString szSDirName = szLocalDirectoryName;
KviQString::ensureLastCharIs(szSDirName,QChar(KVI_PATH_SEPARATOR_CHAR));
szSDirName += szDir;
QString szDDirName = szTargetDirectoryPrefix;
KviQString::ensureLastCharIs(szDDirName,QChar(KVI_PATH_SEPARATOR_CHAR));
szDDirName += szDir;
if(!addDirectory(szSDirName,szDDirName,uAddFileFlags))
return false;
}
#ifndef COMPILE_USE_QT4
++it2;
#endif
}
return true;
}
#define BUFFER_SIZE 32768
bool KviPackageWriter::packFile(KviFile * pFile,DataField * pDataField)
{
QString szProgressText;
KviQString::sprintf(szProgressText,__tr2qs("Packaging file %Q"),&(pDataField->m_szFileLocalName));
if(!updateProgress(m_iCurrentProgress,szProgressText))
return false; // aborted
KviFile source(pDataField->m_szFileLocalName);
if(!source.openForReading())
{
setLastError(__tr2qs("Failed to open a source file for reading"));
return false;
}
kvi_u32_t uSize = source.size();
// Flags
#ifdef COMPILE_ZLIB_SUPPORT
kvi_u32_t uFlags = pDataField->m_bFileAllowCompression ?
(uSize > 64 ? KVI_PACKAGE_DATAFIELD_FLAG_FILE_DEFLATE : 0)
: 0;
#else
kvi_u32_t uFlags = 0;
#endif
if(!pFile->save(uFlags))return writeError();
KviQCString szTargetFileName = KviQString::toUtf8(pDataField->m_szFileTargetName);
// Path
if(!pFile->save(szTargetFileName))return writeError();
kvi_file_offset_t savedSizeOffset = pFile->pos();
// Size : will update it if compression is requested
if(!pFile->save(uSize))return writeError();
pDataField->m_uWrittenFieldLength = 4 + 4 + 4 + szTargetFileName.length(); // sizeof(flags + uncompressed size + path len + path)
// FilePayload
#ifdef COMPILE_ZLIB_SUPPORT
if(uFlags & KVI_PACKAGE_DATAFIELD_FLAG_FILE_DEFLATE)
{
unsigned char ibuffer[BUFFER_SIZE];
unsigned char obuffer[BUFFER_SIZE];
kvi_i32_t iReaded = source.readBlock((char *)ibuffer,BUFFER_SIZE);
if(iReaded < 0)
return readError();
z_stream zstr;
zstr.zalloc = Z_NULL;
zstr.zfree = Z_NULL;
zstr.opaque = Z_NULL;
zstr.next_in = ibuffer;
zstr.avail_in = iReaded;
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
if(deflateInit(&zstr,9) != Z_OK)
{
setLastError(__tr2qs("Compression library initialization error"));
return false;
}
while(iReaded > 0)
{
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
if(deflate(&zstr,Z_NO_FLUSH) != Z_OK)
{
setLastError(__tr2qs("Compression library error"));
return false;
}
if(zstr.avail_out < BUFFER_SIZE)
{
int iCompressed = zstr.next_out - obuffer;
pDataField->m_uWrittenFieldLength += iCompressed;
if(pFile->writeBlock((char *)obuffer,iCompressed) != iCompressed)
{
deflateEnd(&zstr);
return writeError();
}
}
if(zstr.avail_in < BUFFER_SIZE)
{
int iDataToRead = BUFFER_SIZE - zstr.avail_in;
if(iDataToRead < BUFFER_SIZE)
{
if(ibuffer != zstr.next_in)
{
// hum, there is still some data in the buffer to be readed
// and it is not at the beginning...move it to the beginning of ibuffer
memmove(ibuffer,zstr.next_in,zstr.avail_in);
}
}
iReaded = source.readBlock((char *)(ibuffer + zstr.avail_in),iDataToRead);
if(iReaded < 0)
{
deflateEnd(&zstr);
return readError();
}
zstr.avail_in += iReaded;
zstr.next_in = ibuffer;
if((zstr.total_in % 2000000) == 0)
{
QString szTmp;
KviQString::sprintf(szTmp,QString(" (%d of %d bytes)"),zstr.total_in,uSize);
QString szPrg = szProgressText + szTmp;
if(!updateProgress(m_iCurrentProgress,szPrg))
return false; // aborted
}
}
}
// flush pending output
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
int ret;
do
{
ret = deflate(&zstr,Z_FINISH);
if((ret == Z_OK) || (ret == Z_STREAM_END))
{
if(zstr.avail_out < BUFFER_SIZE)
{
int iCompressed = zstr.next_out - obuffer;
pDataField->m_uWrittenFieldLength += iCompressed;
if(pFile->writeBlock((char *)obuffer,iCompressed) != iCompressed)
{
deflateEnd(&zstr);
return writeError();
}
} else {
deflateEnd(&zstr);
setLastError(__tr2qs("Compression library internal error"));
return false;
}
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
}
} while(ret == Z_OK);
// store the compressed data size
kvi_file_offset_t here = pFile->pos();
pFile->seek(savedSizeOffset);
uSize = zstr.total_out;
deflateEnd(&zstr);
if(!pFile->save(uSize))return writeError();
if(ret != Z_STREAM_END)
{
setLastError(__tr2qs("Error while compressing a file stream"));
return false;
}
pFile->seek(here);
} else {
#endif
unsigned char buffer[BUFFER_SIZE];
int iTotalFileSize = 0;
kvi_i32_t iReaded = source.readBlock((char *)buffer,BUFFER_SIZE);
if(iReaded < 0)
return readError();
while(iReaded > 0)
{
iTotalFileSize += iReaded;
if((iTotalFileSize % 1000000) == 0)
{
QString szTmp;
KviQString::sprintf(szTmp,QString(" (%d of %d bytes)"),iTotalFileSize,uSize);
QString szPrg = szProgressText + szTmp;
if(!updateProgress(m_iCurrentProgress,szPrg))
return false; // aborted
}
pDataField->m_uWrittenFieldLength += iReaded;
if(pFile->writeBlock((char *)buffer,iReaded) != iReaded)
return writeError();
iReaded = source.readBlock((char *)buffer,BUFFER_SIZE);
}
#ifdef COMPILE_ZLIB_SUPPORT
}
#endif
source.close();
return true;
}
bool KviPackageWriter::pack(const QString &szFileName,kvi_u32_t uPackFlags)
{
m_iCurrentProgress = 0;
if(!(uPackFlags & NoProgressDialog))
{
showProgressDialog(__tr2qs("Creating package..."),100);
updateProgress(m_iCurrentProgress,__tr2qs("Writing package header"));
}
bool bRet = packInternal(szFileName,uPackFlags);
hideProgressDialog();
return bRet;
}
bool KviPackageWriter::packInternal(const QString &szFileName,kvi_u32_t uPackFlags)
{
KviFile f(szFileName);
if(!f.openForWriting())
{
setLastError(__tr2qs("Can't open file for writing"));
return false;
}
// write the PackageHeader
// Magic
char magic[4];
magic[0] = 'K';
magic[1] = 'V';
magic[2] = 'P';
magic[3] = 'F';
if(f.writeBlock(magic,4) != 4)return writeError();
// Version
kvi_u32_t uVersion = 0x1;
if(!f.save(uVersion))return writeError();
// Flags
kvi_u32_t uFlags = 0x0;
if(!f.save(uFlags))return writeError();
// write PackageInfo
// InfoFieldCount
kvi_u32_t uCount = m_pStringInfoFields->count() + m_pBinaryInfoFields->count();
if(!f.save(uCount))return writeError();
m_iCurrentProgress = 5;
if(!updateProgress(m_iCurrentProgress,__tr2qs("Writing informational fields")))
return false; // aborted
// InfoFields (string)
KviPointerHashTableIterator<QString,QString> it(*m_pStringInfoFields);
while(QString * s = it.current())
{
if(!f.save(it.currentKey()))return writeError();
kvi_u32_t uType = KVI_PACKAGE_INFOFIELD_TYPE_STRING;
if(!f.save(uType))return writeError();
if(!f.save(*s))return writeError();
++it;
}
// InfoFields (binary)
KviPointerHashTableIterator<QString,QByteArray> it2(*m_pBinaryInfoFields);
while(QByteArray * b = it2.current())
{
if(!f.save(it2.currentKey()))return writeError();
kvi_u32_t uType = KVI_PACKAGE_INFOFIELD_TYPE_BINARYBUFFER;
if(!f.save(uType))return writeError();
if(!f.save(*b))return writeError();
++it2;
}
m_iCurrentProgress = 10;
if(!updateProgress(m_iCurrentProgress,__tr2qs("Writing package data")))
return false; // aborted
// write PackageData
int iIdx = 0;
for(DataField * pDataField = m_pDataFields->first();pDataField;pDataField = m_pDataFields->next())
{
kvi_u32_t uDataFieldType = pDataField->m_uType;
if(!f.save(uDataFieldType))return writeError();
kvi_file_offset_t savedLenOffset = f.pos();
// here we will store the length of the field once it's written
if(!f.save(uDataFieldType))return writeError();
m_iCurrentProgress = 10 + ((90 * iIdx) / m_pDataFields->count());
switch(pDataField->m_uType)
{
case KVI_PACKAGE_DATAFIELD_TYPE_FILE:
if(!packFile(&f,pDataField))
return false;
break;
default:
setLastError(__tr2qs("Internal error"));
return false;
break;
}
kvi_file_offset_t savedEndOffset = f.pos();
f.seek(savedLenOffset);
if(!f.save(pDataField->m_uWrittenFieldLength))
return writeError();
f.seek(savedEndOffset);
iIdx++;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Da Reader
KviPackageReader::KviPackageReader()
: KviPackageIOEngine()
{
}
KviPackageReader::~KviPackageReader()
{
}
bool KviPackageReader::readHeaderInternal(KviFile * pFile,const QString &szLocalFileName)
{
// read the PackageHeader
// Magic
char magic[4];
if(pFile->readBlock(magic,4) != 4)return readError();
if((magic[0] != 'K') || (magic[1] != 'V') || (magic[2] != 'P') || (magic[3] != 'F'))
{
setLastError(__tr2qs("The file specified is not a valid KVIrc package"));
return false;
}
// Version
kvi_u32_t uVersion;
if(!pFile->load(uVersion))return readError();
if(uVersion != 0x1)
{
setLastError(__tr2qs("The package has an invalid version number, it might have been created by a newer KVIrc"));
return false;
}
// Flags
kvi_u32_t uFlags;
if(!pFile->load(uFlags))return readError();
// we ignore them at the moment
// read PackageInfo
// InfoFieldCount
kvi_u32_t uCount;
if(!pFile->load(uCount))return writeError();
m_pStringInfoFields->clear();
m_pBinaryInfoFields->clear();
kvi_u32_t uIdx = 0;
while(uIdx < uCount)
{
QString szKey;
if(!pFile->load(szKey))return readError();
kvi_u32_t uFieldType;
if(!pFile->load(uFieldType))return readError();
switch(uFieldType)
{
case KVI_PACKAGE_INFOFIELD_TYPE_STRING:
{
QString szValue;
if(!pFile->load(szValue))return readError();
m_pStringInfoFields->replace(szKey,new QString(szValue));
}
break;
case KVI_PACKAGE_INFOFIELD_TYPE_BINARYBUFFER:
{
QByteArray * pbValue = new QByteArray();
if(!pFile->load(*pbValue))
{
delete pbValue;
return readError();
}
m_pBinaryInfoFields->replace(szKey,pbValue);
}
break;
default:
setLastError(__tr2qs("Invalid info field: the package is probably corrupt"));
break;
}
uIdx++;
}
return true;
}
bool KviPackageReader::readHeader(const QString &szLocalFileName)
{
KviFile f(szLocalFileName);
if(!f.openForReading())
{
setLastError(__tr2qs("Can't open file for reading"));
return false;
}
return readHeaderInternal(&f,szLocalFileName);
}
bool KviPackageReader::unpackFile(KviFile * pFile,const QString &szUnpackPath)
{
// Flags
kvi_u32_t uFlags;
if(!pFile->load(uFlags))return readError();
#ifndef COMPILE_ZLIB_SUPPORT
if(uFlags & KVI_PACKAGE_DATAFIELD_FLAG_FILE_DEFLATE)
{
setLastError(__tr2qs("The package contains compressed data but this executable does not support compression"));
return false;
}
#endif
// Path
QString szPath;
if(!pFile->load(szPath))return readError();
QString szFileName = szUnpackPath;
KviQString::ensureLastCharIs(szFileName,QChar(KVI_PATH_SEPARATOR_CHAR));
szFileName += szPath;
// no attacks please :)
szFileName.replace(QString("..\\"),QString(""));
szFileName.replace(QString("..//"),QString(""));
KviFileUtils::adjustFilePath(szFileName);
int idx = KviQString::findRev(szFileName,QChar(KVI_PATH_SEPARATOR_CHAR));
if(idx != -1)
{
QString szPrefixPath = szFileName.left(idx);
if(!KviFileUtils::makeDir(szPrefixPath))
{
setLastError(__tr2qs("Failed to create the target directory"));
return false;
}
}
KviFile dest(szFileName);
if(!dest.openForWriting())
{
setLastError(__tr2qs("Failed to open a source file for reading"));
return false;
}
QString szProgressText;
KviQString::sprintf(szProgressText,__tr2qs("Unpacking file %Q"),&szFileName);
if(!updateProgress(pFile->pos(),szProgressText))
return false; // aborted
// Size
kvi_u32_t uSize;
if(!pFile->load(uSize))return readError();
// FilePayload
#ifdef COMPILE_ZLIB_SUPPORT
if(uFlags & KVI_PACKAGE_DATAFIELD_FLAG_FILE_DEFLATE)
{
int iRemainingSize = uSize;
unsigned char ibuffer[BUFFER_SIZE];
unsigned char obuffer[BUFFER_SIZE];
int iToRead = iRemainingSize;
if(iToRead > BUFFER_SIZE)iToRead = BUFFER_SIZE;
int iReaded = pFile->readBlock((char *)ibuffer,iToRead);
iRemainingSize -= iReaded;
z_stream zstr;
zstr.zalloc = Z_NULL;
zstr.zfree = Z_NULL;
zstr.opaque = Z_NULL;
zstr.next_in = ibuffer;
zstr.avail_in = iReaded;
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
if(inflateInit(&zstr) != Z_OK)
{
setLastError(__tr2qs("Compression library initialization error"));
return false;
}
while((iReaded > 0) && (iRemainingSize > 0))
{
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
if(inflate(&zstr,Z_NO_FLUSH) != Z_OK)
{
setLastError(__tr2qs("Compression library error"));
return false;
}
if(zstr.avail_out < BUFFER_SIZE)
{
int iDecompressed = zstr.next_out - obuffer;
if(dest.writeBlock((char *)obuffer,iDecompressed) != iDecompressed)
{
inflateEnd(&zstr);
return writeError();
}
}
if(zstr.avail_in < BUFFER_SIZE)
{
int iDataToRead = BUFFER_SIZE - zstr.avail_in;
if(iDataToRead < BUFFER_SIZE)
{
if(ibuffer != zstr.next_in)
{
// hum, there is still some data in the buffer to be readed
// and it is not at the beginning...move it to the beginning of ibuffer
memmove(ibuffer,zstr.next_in,zstr.avail_in);
}
}
if(iDataToRead > iRemainingSize)
iDataToRead = iRemainingSize;
iReaded = pFile->readBlock((char *)(ibuffer + zstr.avail_in),iDataToRead);
if(iReaded < 0)
{
inflateEnd(&zstr);
return readError();
}
iRemainingSize -= iReaded;
zstr.avail_in += iReaded;
zstr.next_in = ibuffer;
if((zstr.total_in % 2000000) == 0)
{
QString szTmp;
KviQString::sprintf(szTmp,QString(" (%d of %d bytes)"),zstr.total_in,uSize);
QString szPrg = szProgressText + szTmp;
if(!updateProgress(pFile->pos(),szPrg))
return false; // aborted
}
}
}
// flush pending output
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
int ret;
do {
ret = inflate(&zstr,Z_FINISH);
if((ret == Z_OK) || (ret == Z_STREAM_END) || (ret == Z_BUF_ERROR))
{
if(zstr.avail_out < BUFFER_SIZE)
{
int iDecompressed = zstr.next_out - obuffer;
if(dest.writeBlock((char *)obuffer,iDecompressed) != iDecompressed)
{
inflateEnd(&zstr);
return writeError();
}
} /* else { THIS HAPPENS FOR ZERO SIZE FILES
debug("hum.... internal, rEWq (ret = %d) (avail_out = %d)",ret,zstr.avail_out);
inflateEnd(&zstr);
setLastError(__tr2qs("Compression library internal error"));
return false;
} */
zstr.next_out = obuffer;
zstr.avail_out = BUFFER_SIZE;
}
} while((ret == Z_OK) || (ret == Z_BUF_ERROR));
inflateEnd(&zstr);
if(ret != Z_STREAM_END)
{
setLastError(__tr2qs("Error in compressed file stream"));
return false;
}
} else {
#endif
unsigned char buffer[BUFFER_SIZE];
int iTotalFileSize = 0;
int iRemainingData = uSize;
int iToRead = iRemainingData;
if(iToRead > BUFFER_SIZE)iToRead = BUFFER_SIZE;
int iReaded = 1;
while((iReaded > 0) && (iToRead > 0))
{
iReaded = pFile->readBlock((char *)buffer,iToRead);
if(iReaded > 0)
{
iTotalFileSize += iReaded;
iRemainingData -= iReaded;
if((iTotalFileSize % 3000000) == 0)
{
QString szTmp;
KviQString::sprintf(szTmp,QString(" (%d of %d bytes)"),iTotalFileSize,uSize);
QString szPrg = szProgressText + szTmp;
if(!updateProgress(pFile->pos(),szPrg))
return false; // aborted
}
if(dest.writeBlock((char *)buffer,iReaded) != iReaded)
return writeError();
}
int iToRead = iRemainingData;
if(iToRead > BUFFER_SIZE)iToRead = BUFFER_SIZE;
}
#ifdef COMPILE_ZLIB_SUPPORT
}
#endif
dest.close();
return true;
}
bool KviPackageReader::getStringInfoField(const QString &szName,QString &szBuffer)
{
QString * pVal = m_pStringInfoFields->find(szName);
if(!pVal)return false;
szBuffer = *pVal;
return true;
}
bool KviPackageReader::unpack(const QString &szLocalFileName,const QString &szUnpackPath,kvi_u32_t uUnpackFlags)
{
bool bRet = unpackInternal(szLocalFileName,szUnpackPath,uUnpackFlags);
hideProgressDialog();
return bRet;
}
bool KviPackageReader::unpackInternal(const QString &szLocalFileName,const QString &szUnpackPath,kvi_u32_t uUnpackFlags)
{
KviFile f(szLocalFileName);
if(!f.openForReading())
{
setLastError(__tr2qs("Can't open file for reading"));
return false;
}
kvi_file_offset_t size = f.size();
if(!(uUnpackFlags & NoProgressDialog))
{
showProgressDialog(__tr2qs("Reading package..."),size);
updateProgress(0,__tr2qs("Reading package header"));
}
if(!readHeaderInternal(&f,szLocalFileName))
return false;
if(!updateProgress(f.pos(),__tr2qs("Reading package data")))
return false; // aborted
while(!f.atEnd())
{
// DataFieldType
kvi_u32_t uDataFieldType;
if(!f.load(uDataFieldType))return readError();
// DataFieldLen
kvi_u32_t uDataFieldLen;
if(!f.load(uDataFieldLen))return readError();
switch(uDataFieldType)
{
case KVI_PACKAGE_DATAFIELD_TYPE_FILE:
if(!unpackFile(&f,szUnpackPath))
return false;
break;
default:
setLastError(__tr2qs("Invalid data field: the package is probably corrupt"));
return false;
break;
}
}
return true;
}
|