forked from pehohlva/wv2qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkzip.h
438 lines (360 loc) · 11.3 KB
/
kzip.h
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
//
// C++ Implementation: KZip to read zip file from ODT or DOCX document on QBuffer
// unzip on Buffer or to dir
// 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
/// this file show my own update on https://github.com/pehohlva/ogeo/blob/master/decompress.h
#ifndef KZIP_H
#define KZIP_H
#define KZIP_ONLY_OPEN = 1
#include "CmdDebug.h"
#include <QtCore/qfile.h>
#include <QtCore/qstring.h>
#include <QtCore/QMap>
#include <QtCore/QDebug>
#include <QtCore/QBuffer>
#include <QtCore/qiodevice.h>
#include <QtCore/qbytearray.h>
#include <QStringList>
#include <QtXml/QDomDocument>
#include <QTextDocument>
#include <QCryptographicHash>
#include <QDate>
#include <QDateTime>
#include <QStringList>
#include <QDomElement>
#include <QBuffer>
#include <QTextCodec>
#include <QFile>
#include <QString>
#include <QDebug>
#include <QDir>
#include <QXmlInputSource>
#include <QXmlSimpleReader>
#include <QDomDocument>
#include <QProcess>
#include <stdio.h>
#include <math.h>
#include <zlib.h>
#include <ctype.h>
/// 6 okt. 2013 save on svn
/// great this to push class on qvariant only by develop validate class!!
template <class T> class VPtr {
public:
static T* asPtr(QVariant v) {
return (T *) v.value<void *>();
}
static QVariant asQVariant(T* ptr) {
return qVariantFromValue((void *) ptr);
}
};
/*
http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/
MyClass *p;
QVariant v = VPtr<MyClass>::asQVariant(p);
MyClass *p1 = VPtr<MyClass>::asPtr(v);
* MMime::MimeTypes help;
*/
class RamBuffer {
public:
RamBuffer(const QString XtypenameCall)
: d(new QBuffer()), name(XtypenameCall) {
d->open(QIODevice::ReadWrite);
}
~RamBuffer() {
d->close();
}
QString type() const {
return name;
}
QDomDocument xmltoken() {
/// QXmlSimpleReader reader;
QXmlInputSource source;
source.setData(d->data());
QString errorMsg;
QDomDocument document;
if (!document.setContent(&source, false, &errorMsg)) {
qWarning() << "QDomDocument err.:" << errorMsg;
return QDomDocument();
}
return document;
}
bool clear() {
d->write(QByteArray());
return d->bytesAvailable() == 0 ? true : false;
}
bool flush_onfile(const QString filedest) {
QFile file(filedest);
if (file.open(QFile::WriteOnly)) {
file.write(d->data(), d->data().length()); // write to stderr
file.close();
return true;
}
}
bool flush_onxmlfile(const QString fullFileName) {
QTextCodec *codectxt = QTextCodec::codecForMib(106);
QFile f(fullFileName);
if (f.open(QFile::WriteOnly | QFile::Text)) {
QTextStream sw(&f);
sw.setCodec(codectxt);
sw << d->data();
f.close();
return true;
}
return false;
}
bool LoadFile(const QString file, int from = -1) {
/// from begin to read at line
//// qDebug() << "### LoadFile from " << from;
bool filled = false;
if (clear()) {
QFile *f = new QFile(file);
if (f->exists()) {
/////
if (f->open(QIODevice::ReadOnly)) {
//// read line by line
int cursorline = -1;
if (f->isReadable()) {
int begin = -1;
if (from > -1) {
begin = from;
}
while (!f->atEnd()) {
cursorline++;
QByteArray crk = f->readLine();
//// qDebug() << "### cursorline:" << cursorline << "\n";
if (cursorline > begin || begin == cursorline) {
d->write(crk);
}
}
f->close();
}
} else {
return false;
//// qDebug() << "######## file errors:" << f->errorString() << "\n";
}
}
}
if (d->bytesAvailable() > 23) {
filled = true;
}
d->seek(0);
return filled;
}
QIODevice *device() {
return d;
}
QByteArray stream() {
return d->data();
}
QString fromUtf8() {
return QString::fromUtf8(stream());
}
protected:
QString name;
QBuffer *d;
};
namespace Tools {
QString f_string76(QString s);
QString UmanTimeFromUnix(uint unixtime);
int dtateint(const QString form, uint unixtime);
QString SimpleMonat(const uint etime);
QString DateUnix(const uint etime);
uint QTime_Null();
QTextCodec *GetcodecfromXml(const QString xmlfile);
QDomDocument format_xml(QByteArray x);
bool _write_file(const QString fullFileName, QByteArray chunk);
bool _write_file(const QString fullFileName, const QString chunk, QByteArray charset);
/// like php strip_tags to get text from html
QByteArray strip_tags(QByteArray istring);
QString fastmd5(const QByteArray xml);
QString TimeNow();
int capture_intbystr(const QString value);
}
#if 0 //// 1 or 0
#define KZIPDEBUG qDebug
#else
#define KZIPDEBUG if (0) qDebug
#endif
namespace KZip {
// zLib authors suggest using larger buffers (128K or 256K) for (de)compression (especially for inflate())
// we use a 256K buffer here - if you want to use this code on a pre-iceage mainframe please change it ;)
#define UNZIP_READ_BUFFER (256*1024)
//// #define KZIPNODEBUGNOW 1
struct LocalFileHeader {
uchar signature[4]; // 0x04034b50
uchar version_needed[2];
uchar general_purpose_bits[2];
uchar compression_method[2];
uchar last_mod_file[4];
uchar crc_32[4];
uchar compressed_size[4];
uchar uncompressed_size[4];
uchar file_name_length[2];
uchar extra_field_length[2];
};
struct DataDescriptor {
uchar crc_32[4];
uchar compressed_size[4];
uchar uncompressed_size[4];
};
struct GentralFileHeader {
uchar signature[4]; // 0x02014b50
uchar version_made[2];
uchar version_needed[2];
uchar general_purpose_bits[2];
uchar compression_method[2];
uchar last_mod_file[4];
uchar crc_32[4];
uchar compressed_size[4];
uchar uncompressed_size[4];
uchar file_name_length[2];
uchar extra_field_length[2];
uchar file_comment_length[2];
uchar disk_start[2];
uchar internal_file_attributes[2];
uchar external_file_attributes[4];
uchar offset_local_header[4];
LocalFileHeader toLocalHeader() const;
};
struct EndOfDirectory {
uchar signature[4]; // 0x06054b50
uchar this_disk[2];
uchar start_of_directory_disk[2];
uchar num_dir_entries_this_disk[2];
uchar num_dir_entries[2];
uchar directory_size[4];
uchar dir_start_offset[4];
uchar comment_length[2];
};
struct FileHeader {
GentralFileHeader h; /// h.compressed_size
QByteArray file_name;
QByteArray extra_field;
QByteArray file_comment;
};
struct FileInfo {
FileInfo();
FileInfo(const FileInfo &other);
~FileInfo();
FileInfo &operator=(const FileInfo &other);
QString filePath;
uint isDir : 1;
uint isFile : 1;
uint isSymLink : 1;
QFile::Permissions permissions;
uint crc32;
qint64 size;
void *d;
};
//// KZip::Stream
class Stream {
public:
Stream(const QString odtfile);
~Stream();
void explode_todir(const QString path, int modus = 0);
QByteArray fileByte(const QString &fileName);
enum ErrorCode {
OkFunky = 1000,
ZlibInit,
ZlibError,
OpenFailed,
PartiallyCorrupted,
Corrupted,
WrongPassword,
NoOpenArchive,
FileNotFound,
ReadFailed,
WriteFailed,
SeekFailed,
HandleCommentHere,
CreateDirFailed,
InvalidDevice,
InvalidArchive,
HeaderConsistencyError,
Skip, SkipAll // internal use only
};
QIODevice *device() {
return d;
}
QByteArray stream() {
return d->data();
}
/// unique id from file
QString md5hash() const {
d->seek(0);
QCryptographicHash formats(QCryptographicHash::Md5);
formats.addData(d->data());
d->seek(0);
return QString(formats.result().toHex().constData());
}
////
bool canread() {
return is_open;
}
/* having list from file and dir and single
* splittet name inside is to null data */
QStringList filelist() const {
return zip_files;
}
/// on debug modus write xml indent readable...
bool write_xml_modus(const QString file, QByteArray x);
/*
Having data filename && chunk QByteArray from his file
*/
QMap<QString, QByteArray> listData() {
return corefilelist; /// having data
}
protected:
char buffer1[UNZIP_READ_BUFFER];
char buffer2[UNZIP_READ_BUFFER];
QList<FileHeader> fileHeaders;
QStringList zip_files;
QByteArray commentario;
uint start_of_directory;
unsigned char* uBuffer;
//// const quint32* crcTable;
quint32 cdOffset;
// End of Central Directory (EOCD) offset
quint32 eocdOffset;
// Number of entries in the Central Directory (as to the EOCD record)
quint16 cdEntryCount;
private:
QMap<QString, QByteArray> corefilelist; /// filename && chunk QByteArray inside
ErrorCode seekToCentralDirectory();
ErrorCode openArchive();
quint32 getULong(const unsigned char* data, quint32 offset) const;
inline quint64 getULLong(const unsigned char* data, quint32 offset) const;
inline quint16 getUShort(const unsigned char* data, quint32 offset) const;
void start() {
d->seek(0);
}
bool clear(); /// remove buffer
bool LoadFile(const QString file);
QBuffer *d;
bool is_open;
};
}
/// SystemSecure::bytesToSize
namespace SystemSecure {
/* Remove a dir and is file recursive is write permission */
bool RM_dir_local(const QString &dirName);
QString bytesToSize(const qint64 size);
qint64 FreespaceonDir(const QString selectdir);
static inline QString freespaceonHome() {
//// return GBxxx...
return bytesToSize(FreespaceonDir(QDir::homePath()));
}
}
namespace OasiCompose {
// remove all attribute on start element or root QDomNode not lost tree
QDomElement cleantag(QDomNode node);
//// import xml file as simple new QDomElement...
QDomElement rendertree(const QByteArray tags);
}
#endif /* KZIP_H */