forked from pehohlva/wv2qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkzip.cpp
1061 lines (923 loc) · 34 KB
/
kzip.cpp
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
//
// C++ Implementation: KZip to read zip file from ODT document on QBuffer
// NOTE only read not write!
// Description:
// idea from qt QZipReader & http://code.mythtv.org/ code
// to build append LIBS += -lz
// Author: Peter Hohl <[email protected]>, 24.10.2013
// http://www.freeroad.ch/
// Copyright: See COPYING file that comes with this distribution
#include <zlib.h>
#include "kzip.h"
#include <QtCore/qdatetime.h>
#include <QtCore/qendian.h>
#include <QtCore/qdebug.h>
#include <QtCore/qdir.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qfile.h>
#include <QtCore/qlist.h>
#include <QtCore/qshareddata.h>
#include <qplatformdefs.h>
#include <QtCore>
#include <QDomDocument>
#include <QTextDocument>
#include <QColor>
#include <QCryptographicHash>
#include <QtCore/qiodevice.h>
#include <QtCore/qbytearray.h>
#include <QDate>
#include <QXmlInputSource>
#include <QXmlSimpleReader>
#include <QDomDocument>
#if defined(Q_OS_SYMBIAN)
#include <f32file.h>
#elif defined(Q_OS_WIN)
#include <windows.h>
#elif defined(Q_OS_MAC)
/// separate on conflict
#endif
//// qt5 + new xcode maverik ...
#if defined(Q_OS_UNIX)
#if !defined(Q_OS_MAC)
#include <sys/vfs.h>
#endif
#endif
#if defined(Q_OS_MAC)
#include <sys/param.h>
#include <sys/mount.h>
#endif
/// only to debug modus!!!
namespace KZip {
//! Local header size (excluding signature, excluding variable length fields)
#define UNZIP_LOCAL_HEADER_SIZE 26
//! Central Directory file entry size (excluding signature, excluding variable length fields)
#define UNZIP_CD_ENTRY_SIZE_NS 42
//! Data descriptor size (excluding signature)
#define UNZIP_DD_SIZE 12
//! End Of Central Directory size (including signature, excluding variable length fields)
#define UNZIP_EOCD_SIZE 22
//! Local header entry encryption header size
#define UNZIP_LOCAL_ENC_HEADER_SIZE 12
// Some offsets inside a CD record (excluding signature)
#define UNZIP_CD_OFF_VERSION_MADE 0
#define UNZIP_CD_OFF_VERSION 2
#define UNZIP_CD_OFF_GPFLAG 4
#define UNZIP_CD_OFF_CMETHOD 6
#define UNZIP_CD_OFF_MODT 8
#define UNZIP_CD_OFF_MODD 10
#define UNZIP_CD_OFF_CRC32 12
#define UNZIP_CD_OFF_CSIZE 16
#define UNZIP_CD_OFF_USIZE 20
#define UNZIP_CD_OFF_NAMELEN 24
#define UNZIP_CD_OFF_XLEN 26
#define UNZIP_CD_OFF_COMMLEN 28
#define UNZIP_CD_OFF_LHOFFSET 38
// Some offsets inside a local header record (excluding signature)
#define UNZIP_LH_OFF_VERSION 0
#define UNZIP_LH_OFF_GPFLAG 2
#define UNZIP_LH_OFF_CMETHOD 4
#define UNZIP_LH_OFF_MODT 6
#define UNZIP_LH_OFF_MODD 8
#define UNZIP_LH_OFF_CRC32 10
#define UNZIP_LH_OFF_CSIZE 14
#define UNZIP_LH_OFF_USIZE 18
#define UNZIP_LH_OFF_NAMELEN 22
#define UNZIP_LH_OFF_XLEN 24
// Some offsets inside a data descriptor record (excluding signature)
#define UNZIP_DD_OFF_CRC32 0
#define UNZIP_DD_OFF_CSIZE 4
#define UNZIP_DD_OFF_USIZE 8
// Some offsets inside a EOCD record
#define UNZIP_EOCD_OFF_ENTRIES 6
#define UNZIP_EOCD_OFF_CDOFF 12
#define UNZIP_EOCD_OFF_COMMLEN 16
/*!
Max version handled by this API.
0x14 = 2.0 --> full compatibility only up to this version;
later versions use unsupported features
*/
#define UNZIP_VERSION 0x14
static quint32 permissionsToMode(QFile::Permissions perms) {
quint32 mode = 0;
if (perms & QFile::ReadOwner)
mode |= S_IRUSR;
if (perms & QFile::WriteOwner)
mode |= S_IWUSR;
if (perms & QFile::ExeOwner)
mode |= S_IXUSR;
if (perms & QFile::ReadUser)
mode |= S_IRUSR;
if (perms & QFile::WriteUser)
mode |= S_IWUSR;
if (perms & QFile::ExeUser)
mode |= S_IXUSR;
if (perms & QFile::ReadGroup)
mode |= S_IRGRP;
if (perms & QFile::WriteGroup)
mode |= S_IWGRP;
if (perms & QFile::ExeGroup)
mode |= S_IXGRP;
if (perms & QFile::ReadOther)
mode |= S_IROTH;
if (perms & QFile::WriteOther)
mode |= S_IWOTH;
if (perms & QFile::ExeOther)
mode |= S_IXOTH;
return mode;
}
static QFile::Permissions modeToPermissions(quint32 mode) {
QFile::Permissions ret;
if (mode & S_IRUSR)
ret |= QFile::ReadOwner;
if (mode & S_IWUSR)
ret |= QFile::WriteOwner;
if (mode & S_IXUSR)
ret |= QFile::ExeOwner;
if (mode & S_IRUSR)
ret |= QFile::ReadUser;
if (mode & S_IWUSR)
ret |= QFile::WriteUser;
if (mode & S_IXUSR)
ret |= QFile::ExeUser;
if (mode & S_IRGRP)
ret |= QFile::ReadGroup;
if (mode & S_IWGRP)
ret |= QFile::WriteGroup;
if (mode & S_IXGRP)
ret |= QFile::ExeGroup;
if (mode & S_IROTH)
ret |= QFile::ReadOther;
if (mode & S_IWOTH)
ret |= QFile::WriteOther;
if (mode & S_IXOTH)
ret |= QFile::ExeOther;
return ret;
}
static inline uint readUInt(const uchar *data) {
return (data[0]) + (data[1] << 8) + (data[2] << 16) + (data[3] << 24);
}
static inline ushort readUShort(const uchar *data) {
return (data[0]) + (data[1] << 8);
}
static inline void writeUInt(uchar *data, uint i) {
data[0] = i & 0xff;
data[1] = (i >> 8) & 0xff;
data[2] = (i >> 16) & 0xff;
data[3] = (i >> 24) & 0xff;
}
static inline void writeUShort(uchar *data, ushort i) {
data[0] = i & 0xff;
data[1] = (i >> 8) & 0xff;
}
static int deflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen) {
z_stream stream;
int err;
stream.next_in = (Bytef*) source;
stream.avail_in = (uInt) sourceLen;
stream.next_out = dest;
stream.avail_out = (uInt) * destLen;
if ((uLong) stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func) 0;
stream.zfree = (free_func) 0;
stream.opaque = (voidpf) 0;
err = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
if (err != Z_OK) return err;
err = deflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&stream);
return err == Z_OK ? Z_BUF_ERROR : err;
}
*destLen = stream.total_out;
err = deflateEnd(&stream);
return err;
}
static void writeMSDosDate(uchar *dest, const QDateTime& dt) {
if (dt.isValid()) {
quint16 time =
(dt.time().hour() << 11) // 5 bit hour
| (dt.time().minute() << 5) // 6 bit minute
| (dt.time().second() >> 1); // 5 bit double seconds
dest[0] = time & 0xff;
dest[1] = time >> 8;
quint16 date =
((dt.date().year() - 1980) << 9) // 7 bit year 1980-based
| (dt.date().month() << 5) // 4 bit month
| (dt.date().day()); // 5 bit day
dest[2] = char(date);
dest[3] = char(date >> 8);
} else {
dest[0] = 0;
dest[1] = 0;
dest[2] = 0;
dest[3] = 0;
}
}
static int inflate(Bytef *dest, ulong *destLen, const Bytef *source, ulong sourceLen) {
z_stream stream;
int err;
stream.next_in = (Bytef*) source;
stream.avail_in = (uInt) sourceLen;
if ((uLong) stream.avail_in != sourceLen)
return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = (uInt) * destLen;
if ((uLong) stream.avail_out != *destLen)
return Z_BUF_ERROR;
stream.zalloc = (alloc_func) 0;
stream.zfree = (free_func) 0;
err = inflateInit2(&stream, -MAX_WBITS);
if (err != Z_OK)
return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
return Z_DATA_ERROR;
return err;
}
*destLen = stream.total_out;
err = inflateEnd(&stream);
return err;
}
/// Stream
#define ZIP_VERSION 20
Stream::Stream(const QString odtfile) {
d = new QBuffer();
ErrorCode ec = ReadFailed;
uBuffer = (unsigned char*) buffer1;
//// crcTable = (quint32*) get_crc_table(); /// zlib
is_open = false;
if (d->open(QIODevice::ReadWrite)) {
is_open = LoadFile(odtfile);
ec = seekToCentralDirectory();
////qDebug() << "### seekToCentralDirectory return ... " << ec;
if (ec != Stream::OkFunky) {
is_open = false;
}
if (cdEntryCount == 0) {
is_open = false;
}
}
if (!is_open) {
clear(); // remove buffer
} else {
ec = openArchive();
if (ec == Stream::OkFunky) {
/// KZIPDEBUG() << "open file decompress.......";
int i;
QStringList pieces;
for (i = 0; i < fileHeaders.size(); ++i) {
FileHeader metaheader = fileHeaders.at(i);
const QString zfile = QString::fromLocal8Bit(metaheader.file_name);
if (zfile.indexOf("/")) {
QStringList subdir = zfile.split("/");
pieces << subdir;
}
QByteArray chunk = fileByte(zfile);
zip_files << zfile;
if (chunk.size() > 3) {
corefilelist.insert(zfile, chunk);
////KZIPDEBUG() << "contenent size()=" << chunk.size();
}
//// KZIPDEBUG() << "cat ." << i << " - " << zfile;
}
zip_files << QString("##items##");
zip_files << pieces;
zip_files.removeDuplicates();
}
}
}
QByteArray Stream::fileByte(const QString &fileName) {
int compressed_size = 0;
int uncompressed_size = 0;
int i;
bool found = false;
for (i = 0; i < fileHeaders.size(); ++i) {
if (QString::fromLocal8Bit(fileHeaders.at(i).file_name) == fileName) {
found = true;
break;
}
}
start(); /// seek 0;
if (!found || !device()) {
return QByteArray();
}
FileHeader metaheader = fileHeaders.at(i);
compressed_size = readUInt(metaheader.h.compressed_size);
uncompressed_size = readUInt(metaheader.h.uncompressed_size);
if (uncompressed_size < 1) {
return QByteArray();
}
int start = readUInt(metaheader.h.offset_local_header);
device()->seek(start);
LocalFileHeader lh;
device()->read((char *) &lh, sizeof (LocalFileHeader));
uint skip = readUShort(lh.file_name_length) + readUShort(lh.extra_field_length);
device()->seek(device()->pos() + skip);
int compression_method = readUShort(lh.compression_method);
QByteArray compressed = device()->read(compressed_size);
const QString zfile = QString::fromLocal8Bit(metaheader.file_name);
KZIPDEBUG() << "|||" << compression_method << "|||| fileByte file " << zfile << " s:" << uncompressed_size << ":" << compressed_size;
if (compression_method == 0) {
// no compression
compressed.truncate(uncompressed_size);
return compressed;
} else if (compression_method == 8) {
/// real unzip part file
compressed.truncate(compressed_size);
QByteArray decompress_chunk;
ulong len = qMax(uncompressed_size, 1);
int res;
do {
decompress_chunk.resize(len);
res = inflate((uchar*) decompress_chunk.data(), &len, (uchar*) compressed.constData(), compressed_size);
if (res == Z_OK) {
if ((int) len != decompress_chunk.size()) {
decompress_chunk.resize(len);
}
break;
} else {
decompress_chunk.clear();
qWarning("KZip: Z_DATA_ERROR: Input data is corrupted");
}
} while (res == Z_BUF_ERROR);
return decompress_chunk;
}
qWarning() << "KZip: Unknown compression method";
return QByteArray();
}
Stream::ErrorCode Stream::openArchive() {
Q_ASSERT(device());
if (!canread()) {
qDebug() << "Unable to open device for reading";
return Stream::OpenFailed;
}
start(); /// seek 0;
uchar tmp[4];
device()->read((char *) tmp, 4);
if (getULong(tmp, 0) != 0x04034b50) {
qWarning() << "KZip: not a zip file!";
return Stream::OpenFailed;
}
// find EndOfDirectory header
int i = 0;
int start_of_directory = -1;
int num_dir_entries = 0;
EndOfDirectory eod;
while (start_of_directory == -1) {
int pos = device()->size() - sizeof (EndOfDirectory) - i;
if (pos < 0 || i > 65535) {
qWarning() << "KZip: EndOfDirectory not found";
return Stream::OpenFailed;
}
device()->seek(pos);
device()->read((char *) &eod, sizeof (EndOfDirectory));
if (readUInt(eod.signature) == 0x06054b50)
break;
++i;
}
start_of_directory = readUInt(eod.dir_start_offset);
num_dir_entries = readUShort(eod.num_dir_entries);
if (cdEntryCount != num_dir_entries) {
return Stream::OpenFailed;
}
KZIPDEBUG("start_of_directory at %d, num_dir_entries=%d", start_of_directory, num_dir_entries);
int comment_length = readUShort(eod.comment_length);
if (comment_length != i) {
qWarning() << "KZip: failed to parse zip file.";
return Stream::OpenFailed;
}
commentario = device()->read(qMin(comment_length, i));
///// KZIPDEBUG() << "### comment_length:" << comment_length;
device()->seek(start_of_directory);
for (i = 0; i < num_dir_entries; ++i) {
FileHeader header;
int read = device()->read((char *) &header.h, sizeof (GentralFileHeader));
if (read < (int) sizeof (GentralFileHeader)) {
qWarning() << "KZip: Failed to read complete header, index may be incomplete";
break;
}
if (readUInt(header.h.signature) != 0x02014b50) {
qWarning() << "KZip: invalid header signature, index may be incomplete";
break;
}
int l = readUShort(header.h.file_name_length);
header.file_name = device()->read(l);
if (header.file_name.length() != l) {
qWarning() << "KZip: Failed to read filename from zip index, index may be incomplete";
break;
}
l = readUShort(header.h.extra_field_length);
header.extra_field = device()->read(l);
if (header.extra_field.length() != l) {
qWarning() << "KZip: Failed to read extra field in zip file, skipping file, index may be incomplete";
break;
}
l = readUShort(header.h.file_comment_length);
header.file_comment = device()->read(l);
if (header.file_comment.length() != l) {
qWarning() << "KZip: Failed to read read file comment, index may be incomplete";
break;
}
//// KZIPDEBUG() << "### pos:" << i; /// header.h.compressed_size
//// KZIPDEBUG("found at file:{%s}", header.file_name.data());
fileHeaders.append(header);
}
return Stream::OkFunky;
}
Stream::ErrorCode Stream::seekToCentralDirectory() {
Q_ASSERT(device());
qint64 length = device()->size();
qint64 offset = length - UNZIP_EOCD_SIZE;
if (length < UNZIP_EOCD_SIZE) {
return Stream::InvalidArchive;
}
if (!device()->seek(offset)) {
return Stream::SeekFailed;
}
if (device()->read(buffer1, UNZIP_EOCD_SIZE) != UNZIP_EOCD_SIZE) {
return Stream::ReadFailed;
}
bool eocdFound = (buffer1[0] == 'P' && buffer1[1] == 'K' && buffer1[2] == 0x05 && buffer1[3] == 0x06);
if (eocdFound) {
// Zip file has no comment (the only variable length field in the EOCD record)
eocdOffset = offset;
} else {
return Stream::HandleCommentHere;
/*
qint64 read;
char* p = 0;
offset -= UNZIP_EOCD_SIZE;
if (offset <= 0) {
return Stream::InvalidArchive;
}
if (!device()->seek(offset)) {
return Stream::SeekFailed;
}
int cursor =-1;
while ((read = device()->read(buffer1, UNZIP_EOCD_SIZE)) >= 0) {
cursor++;
qDebug() << "### cursor:" << cursor << "|";
}
* */
}
if (!eocdFound) {
return Stream::InvalidArchive;
}
// Parse EOCD to locate CD offset
offset = getULong((const unsigned char*) buffer1, UNZIP_EOCD_OFF_CDOFF + 4);
cdOffset = offset;
cdEntryCount = getUShort((const unsigned char*) buffer1, UNZIP_EOCD_OFF_ENTRIES + 4);
quint16 commentLength = getUShort((const unsigned char*) buffer1, UNZIP_EOCD_OFF_COMMLEN + 4);
if (commentLength != 0) {
return Stream::HandleCommentHere;
}
if (!device()->seek(cdOffset)) {
return Stream::SeekFailed;
}
return Stream::OkFunky;
}
bool Stream::LoadFile(const QString file) {
if (clear()) {
QFile f(file);
if (f.exists()) {
if (f.open(QFile::ReadOnly)) {
d->write(f.readAll());
f.close();
start();
return true;
}
}
}
return false;
}
void Stream::explode_todir(const QString path, int modus) {
QDir dir(path);
if (!dir.exists(path)) {
return;
}
int filecount = 0;
const QString ziplocaldir = dir.canonicalPath();
QMapIterator<QString, QByteArray> i(corefilelist);
while (i.hasNext()) {
i.next();
const QString singlefile = ziplocaldir + "/" + QString(i.key());
QFileInfo pfile(singlefile);
const QString ext = pfile.completeSuffix().toLower();
const QString d_path = pfile.absolutePath();
if (!dir.exists(d_path)) {
dir.mkpath(d_path);
}
if (pfile.absolutePath() != pfile.absoluteFilePath()) {
QByteArray data = i.value();
// _write_file
bool ok = write_xml_modus(pfile.absoluteFilePath(), data);
if (!ok) {
if (modus == 1) {
RamBuffer *bufferbin = new RamBuffer("tmpbinary");
bufferbin->device()->write(data);
bufferbin->flush_onfile(pfile.absoluteFilePath());
bufferbin->clear();
} else {
qDebug() << "Warning to write NOT xml:" << pfile.absoluteFilePath();
}
} else {
filecount++;
}
}
}
qDebug() << "KZip Writtel total " << filecount << " file on path:" << path;
}
bool Stream::write_xml_modus(const QString file, QByteArray x) {
/// debug modus
QFileInfo fi_c(file);
const QString ext = fi_c.completeSuffix().toLower();
if (ext == "xml") {
RamBuffer *buffer = new RamBuffer("tmpxml");
buffer->device()->write(x);
QDomDocument doc = buffer->xmltoken();
QString xml = doc.toString(5);
buffer->clear();
return Tools::_write_file(file, xml.toLocal8Bit(), "utf-8");
}
return false;
}
/*!
\internal Reads an quint16 (2 bytes) from a byte array starting at given offset.
*/
quint16 Stream::getUShort(const unsigned char* data, quint32 offset) const {
return (quint16) data[offset] | (((quint16) data[offset + 1]) << 8);
}
/*!
\internal Reads an quint64 (8 bytes) from a byte array starting at given offset.
*/
quint64 Stream::getULLong(const unsigned char* data, quint32 offset) const {
quint64 res = (quint64) data[offset];
res |= (((quint64) data[offset + 1]) << 8);
res |= (((quint64) data[offset + 2]) << 16);
res |= (((quint64) data[offset + 3]) << 24);
res |= (((quint64) data[offset + 1]) << 32);
res |= (((quint64) data[offset + 2]) << 40);
res |= (((quint64) data[offset + 3]) << 48);
res |= (((quint64) data[offset + 3]) << 56);
return res;
}
/*!
\internal Reads an quint32 (4 bytes) from a byte array starting at given offset.
*/
quint32 Stream::getULong(const unsigned char* data, quint32 offset) const {
quint32 res = (quint32) data[offset];
res |= (((quint32) data[offset + 1]) << 8);
res |= (((quint32) data[offset + 2]) << 16);
res |= (((quint32) data[offset + 3]) << 24);
return res;
}
bool Stream::clear() {
d->write(QByteArray());
start();
return d->bytesAvailable() == 0 ? true : false;
}
/*!
Desctructor
*/
Stream::~Stream() {
clear();
delete d;
}
}
namespace Tools {
QDomDocument format_xml(QByteArray x) {
QXmlSimpleReader reader;
QXmlInputSource source;
source.setData(x);
QString errorMsg;
QDomDocument document;
if (!document.setContent(&source, &reader, &errorMsg)) {
return QDomDocument();
}
return document;
}
QString f_string76(QString s) {
///
QString repair = "";
qint64 o = 0;
for (int i = 0; i < s.size(); ++i) {
o++;
repair.append(s.at(i));
if (o == 2000) {
o = 0;
repair.append(QString("\n"));
}
}
return repair;
}
/* time null unix time long nummer */
uint QTime_Null() {
QDateTime timer1(QDateTime::currentDateTime());
return timer1.toTime_t();
}
//// DateUnix(QTime_Null())
QString DateUnix(const uint etime) {
QDateTime start = QDateTime::currentDateTime();
start.setTime_t(etime);
if (start.toString("HH:mm") == "00:00") {
return start.toString("dd.MM.yyyy");
} else {
return start.toString("dd.MM.yyyy HH:mm");
}
}
QString SimpleMonat(const uint etime) {
QDateTime start = QDateTime::currentDateTime();
start.setTime_t(etime);
return start.toString("dd.MM.");
}
int capture_intbystr(const QString value) {
QString istring = value;
QString try_2int;
const int fx = istring.length();
for (int i = 0; i < fx; i++) {
QChar vox(istring.at(i));
if (vox.digitValue() != -1) {
try_2int += istring.at(i);
}
}
if (try_2int.size() < 1) {
return -1;
}
bool ok;
int result = try_2int.toInt(&ok);
if (ok) {
return result;
} else {
return -1;
}
}
/* return int value from a unixtime date MMM YYY ... */
int dtateint(const QString form, uint unixtime) {
QDateTime fromunix;
fromunix.setTime_t(unixtime);
QString numeric = fromunix.toString((const QString) form);
bool ok;
const int rec = numeric.toInt(&ok);
if (ok) {
return rec;
}
return (0);
}
//// QDateTime fromunix;
/// fromunix.setTime_t(unixtime);
/* display a mail date format UmanTimeFromUnix(QTime_Null()) */
QString UmanTimeFromUnix(uint unixtime) {
/* mail rtf Date format! http://www.faqs.org/rfcs/rfc788.html */
QDateTime fromunix;
bool ok;
QDateTime now = QDateTime::currentDateTime();
fromunix.setTime_t(unixtime);
QStringList RTFdays = QStringList() << "day_NULL" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat" << "Sun";
QStringList RTFmonth = QStringList() << "month_NULL" << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
int anno = fromunix.toString("yyyy").toInt(&ok);
int mese = fromunix.toString("M").toInt(&ok);
int giorno = fromunix.toString("d").toInt(&ok);
QDate timeroad(anno, mese, giorno);
QStringList rtfd_line;
rtfd_line.clear();
if (!ok) {
rtfd_line.append("Error Time! ");
} else {
rtfd_line.append("Date: ");
}
rtfd_line.append(RTFdays.at(timeroad.dayOfWeek()));
rtfd_line.append(", ");
rtfd_line.append(QString::number(giorno));
rtfd_line.append(" ");
rtfd_line.append(RTFmonth.at(mese));
rtfd_line.append(" ");
rtfd_line.append(QString::number(anno));
rtfd_line.append(" ");
rtfd_line.append(now.toString("hh:mm:ss:zzz"));
rtfd_line.append("");
/*qDebug() << "### mail rtf Date format " << rtfd_line.join("");*/
return QString(rtfd_line.join(""));
}
/* correct codex from xml file read only first line */
QTextCodec *GetcodecfromXml(const QString xmlfile) {
QString semencoding = "UTF-8";
QTextCodec *codecsin;
QFile *xfile = new QFile(xmlfile);
if (!xfile->exists()) {
codecsin = QTextCodec::codecForName(semencoding.toLocal8Bit());
return codecsin;
}
QString Firstline;
bool validxml = false;
if (xfile->open(QIODevice::ReadOnly)) {
char buf[1024];
qint64 lineLength = xfile->readLine(buf, sizeof (buf));
Firstline = QString(buf);
if (lineLength > 10 && Firstline.contains("encoding")) {
validxml = true;
}
}
if (!validxml) {
codecsin = QTextCodec::codecForName(semencoding.toLocal8Bit());
return codecsin;
}
QRegExp expression("encoding=[\"\'](.*)[\"\']", Qt::CaseInsensitive);
expression.setMinimal(true);
int iPosition = 0;
while ((iPosition = expression.indexIn(Firstline, iPosition)) != -1) {
semencoding = expression.cap(0);
semencoding = semencoding.mid(10, semencoding.size() - 11);
iPosition += expression.matchedLength();
//// qDebug() << "### semencoding" << semencoding;
}
if (iPosition == -1) {
codecsin = QTextCodec::codecForName("UTF-8");
} else {
codecsin = QTextCodec::codecForName(semencoding.toLocal8Bit());
}
return codecsin;
}
bool _write_file(const QString fullFileName, QByteArray chunk) {
QFile file(fullFileName);
if (file.open(QFile::WriteOnly)) {
file.write(chunk, chunk.length()); // write to stderr
file.close();
return true;
}
return false;
}
/* write a file to utf-8 format */
bool _write_file(const QString fullFileName, const QString chunk, QByteArray charset) {
////QTextCodec *codecx;
//// QTextCodec *codectxt = QTextCodec::codecForMib(106);
QTextCodec *codectxt = QTextCodec::codecForName(charset);
if (!codectxt) {
return false;
}
QFile f(fullFileName);
if (f.open(QFile::WriteOnly | QFile::Text)) {
QTextStream sw(&f);
sw.setCodec(codectxt);
sw << chunk;
f.close();
return true;
}
return false;
}
QByteArray strip_tags(QByteArray istring) {
bool intag = false;
QByteArray new_string;
for (int i = 0; i < istring.length(); i++) {
QChar vox(istring.at(i));
int letter = vox.unicode(); /// <60 62>
if (letter != 60 && !intag) {
new_string += istring.at(i);
}
if (letter == 60 && !intag) {
intag = true;
}
if (letter == 62 && intag) {
intag = false;
}
}
return new_string;
}
QString fastmd5(const QByteArray xml) {
QCryptographicHash formats(QCryptographicHash::Md5);
formats.addData(xml);
return QString(formats.result().toHex().constData());
}
QString TimeNow() {
return UmanTimeFromUnix(QTime_Null());
}
}
namespace OasiCompose {
QDomElement cleantag(QDomNode node) {
QDomElement reparse = node.toElement();
QStringList hlist;
QDomNamedNodeMap attlist = reparse.attributes();
const int sizeatt = attlist.count();
for (int i = 0; i < sizeatt; i++) {
QDomNode nod = attlist.item(i);
const QString attname = nod.nodeName();
hlist << attname;
}
for (int x = 0; x < hlist.size(); x++) {
QString item = hlist.at(x);
reparse.removeAttribute(item);
}
return reparse;
}
QDomElement rendertree(const QByteArray tags) {
RamBuffer *bxs = new RamBuffer("tmp_xml_stream");
bxs->device()->write(tags);
QDomDocument doc_tmp = bxs->xmltoken();
QDomElement root_001 = doc_tmp.documentElement();
QDomNode tree_001 = doc_tmp.importNode(root_001, true);
return OasiCompose::cleantag(tree_001);
}
}
namespace SystemSecure {
bool RM_dir_local(const QString &dirName) {
bool result = true;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = RM_dir_local(info.absoluteFilePath());
} else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
//// 1355776
QString bytesToSize(const qint64 size) {
if (size < 0)
return QString();
if (size < 1024)
return QObject::tr("%1 B").arg(QString::number(((double) size), 'f', 0));
if ((size >= 1024) && (size < 1048576))
return QObject::tr("%1 KB").arg(QString::number(((double) size) / 1024, 'f', 0));
if ((size >= 1048576) && (size < 1073741824))
return QObject::tr("%1 MB").arg(QString::number(((double) size) / 1048576, 'f', 2));
if (size >= 1073741824)
return QObject::tr("%1 GB").arg(QString::number(((double) size) / 1073741824, 'f', 2));
return QString();
}
qint64 FreespaceonDir(const QString selectdir) {
#if !defined(Q_OS_WIN)
struct statfs stats;
statfs(selectdir.toLocal8Bit(), &stats);
unsigned long long bavail = ((unsigned long long) stats.f_bavail);
unsigned long long bsize = ((unsigned long long) stats.f_bsize);
return (qint64) (bavail * bsize);
#else
// MS recommend the use of GetDiskFreeSpaceEx, but this is not available on early versions
// of windows 95. GetDiskFreeSpace is unable to report free space larger than 2GB, but we're
// only concerned with much smaller amounts of free space, so this is not a hindrance.
DWORD bytesPerSector(0);
DWORD sectorsPerCluster(0);
DWORD freeClusters(0);
DWORD totalClusters(0);
if (::GetDiskFreeSpace(selectdir.utf16(), &bytesPerSector, §orsPerCluster, &freeClusters, &totalClusters) == FALSE) {
qWarning() << "Unable to get free disk space on:" << selectdir;
}
return (qint64) ((bytesPerSector * sectorsPerCluster * freeClusters) > boundary);
#endif
return 0;
/*
* #if defined(Q_OS_SYMBIAN)
bool result(false);
RFs fsSession;
TInt rv;
if ((rv = fsSession.Connect()) != KErrNone) {
qDebug() << "Unable to connect to FS:" << rv;
} else {