1 /* This file is part of the KDE libraries
2 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
3 SPDX-FileCopyrightText: 2002 Holger Schroeder <holger-kde@holgis.net>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8 #include "kzip.h"
9 #include "karchive_p.h"
10 #include "kcompressiondevice.h"
11 #include "klimitediodevice_p.h"
12 #include "loggingcategory.h"
13
14 #include <QByteArray>
15 #include <QDate>
16 #include <QDebug>
17 #include <QDir>
18 #include <QFile>
19 #include <QHash>
20 #include <QList>
21 #include <qplatformdefs.h>
22
23 #include <string.h>
24 #include <time.h>
25 #include <zlib.h>
26
27 #ifndef QT_STAT_LNK
28 #define QT_STAT_LNK 0120000
29 #endif // QT_STAT_LNK
30
31 static const int max_path_len = 4095; // maximum number of character a path may contain
32
transformToMsDos(const QDateTime & _dt,char * buffer)33 static void transformToMsDos(const QDateTime &_dt, char *buffer)
34 {
35 const QDateTime dt = _dt.isValid() ? _dt : QDateTime::currentDateTime();
36 /* clang-format off */
37 const quint16 time = (dt.time().hour() << 11) // 5 bit hour
38 | (dt.time().minute() << 5) // 6 bit minute
39 | (dt.time().second() >> 1); // 5 bit double seconds
40 /* clang-format on */
41
42 buffer[0] = char(time);
43 buffer[1] = char(time >> 8);
44
45 /* clang-format off */
46 const quint16 date = ((dt.date().year() - 1980) << 9) // 7 bit year 1980-based
47 | (dt.date().month() << 5) // 4 bit month
48 | (dt.date().day()); // 5 bit day
49 /* clang-format on */
50
51 buffer[2] = char(date);
52 buffer[3] = char(date >> 8);
53 }
54
transformFromMsDos(const char * buffer)55 static uint transformFromMsDos(const char *buffer)
56 {
57 quint16 time = (uchar)buffer[0] | ((uchar)buffer[1] << 8);
58 int h = time >> 11;
59 int m = (time & 0x7ff) >> 5;
60 int s = (time & 0x1f) * 2;
61 QTime qt(h, m, s);
62
63 quint16 date = (uchar)buffer[2] | ((uchar)buffer[3] << 8);
64 int y = (date >> 9) + 1980;
65 int o = (date & 0x1ff) >> 5;
66 int d = (date & 0x1f);
67 QDate qd(y, o, d);
68
69 QDateTime dt(qd, qt);
70 return dt.toSecsSinceEpoch();
71 }
72
73 // == parsing routines for zip headers
74
75 /** all relevant information about parsing file information */
76 struct ParseFileInfo {
77 // file related info
78 mode_t perm; // permissions of this file
79 // TODO: use quint32 instead of a uint?
80 uint atime; // last access time (UNIX format)
81 uint mtime; // modification time (UNIX format)
82 uint ctime; // creation time (UNIX format)
83 int uid; // user id (-1 if not specified)
84 int gid; // group id (-1 if not specified)
85 QByteArray guessed_symlink; // guessed symlink target
86 int extralen; // length of extra field
87
88 // parsing related info
89 bool exttimestamp_seen; // true if extended timestamp extra field
90 // has been parsed
91 bool newinfounix_seen; // true if Info-ZIP Unix New extra field has
92 // been parsed
93
ParseFileInfoParseFileInfo94 ParseFileInfo()
95 : perm(0100644)
96 , uid(-1)
97 , gid(-1)
98 , extralen(0)
99 , exttimestamp_seen(false)
100 , newinfounix_seen(false)
101 {
102 ctime = mtime = atime = time(nullptr);
103 }
104 };
105
106 /** updates the parse information with the given extended timestamp extra field.
107 * @param buffer start content of buffer known to contain an extended
108 * timestamp extra field (without magic & size)
109 * @param size size of field content (must not count magic and size entries)
110 * @param islocal true if this is a local field, false if central
111 * @param pfi ParseFileInfo object to be updated
112 * @return true if processing was successful
113 */
parseExtTimestamp(const char * buffer,int size,bool islocal,ParseFileInfo & pfi)114 static bool parseExtTimestamp(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
115 {
116 if (size < 1) {
117 // qCDebug(KArchiveLog) << "premature end of extended timestamp (#1)";
118 return false;
119 } /*end if*/
120 int flags = *buffer; // read flags
121 buffer += 1;
122 size -= 1;
123
124 if (flags & 1) { // contains modification time
125 if (size < 4) {
126 // qCDebug(KArchiveLog) << "premature end of extended timestamp (#2)";
127 return false;
128 } /*end if*/
129 pfi.mtime = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
130 buffer += 4;
131 size -= 4;
132 } /*end if*/
133 // central extended field cannot contain more than the modification time
134 // even if other flags are set
135 if (!islocal) {
136 pfi.exttimestamp_seen = true;
137 return true;
138 } /*end if*/
139
140 if (flags & 2) { // contains last access time
141 if (size < 4) {
142 // qCDebug(KArchiveLog) << "premature end of extended timestamp (#3)";
143 return true;
144 } /*end if*/
145 pfi.atime = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
146 buffer += 4;
147 size -= 4;
148 } /*end if*/
149
150 if (flags & 4) { // contains creation time
151 if (size < 4) {
152 // qCDebug(KArchiveLog) << "premature end of extended timestamp (#4)";
153 return true;
154 } /*end if*/
155 pfi.ctime = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
156 buffer += 4;
157 } /*end if*/
158
159 pfi.exttimestamp_seen = true;
160 return true;
161 }
162
163 /** updates the parse information with the given Info-ZIP Unix old extra field.
164 * @param buffer start of content of buffer known to contain an Info-ZIP
165 * Unix old extra field (without magic & size)
166 * @param size size of field content (must not count magic and size entries)
167 * @param islocal true if this is a local field, false if central
168 * @param pfi ParseFileInfo object to be updated
169 * @return true if processing was successful
170 */
parseInfoZipUnixOld(const char * buffer,int size,bool islocal,ParseFileInfo & pfi)171 static bool parseInfoZipUnixOld(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
172 {
173 // spec mandates to omit this field if one of the newer fields are available
174 if (pfi.exttimestamp_seen || pfi.newinfounix_seen) {
175 return true;
176 }
177
178 if (size < 8) {
179 // qCDebug(KArchiveLog) << "premature end of Info-ZIP unix extra field old";
180 return false;
181 }
182
183 pfi.atime = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
184 buffer += 4;
185 pfi.mtime = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24);
186 buffer += 4;
187 if (islocal && size >= 12) {
188 pfi.uid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
189 buffer += 2;
190 pfi.gid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
191 buffer += 2;
192 } /*end if*/
193 return true;
194 }
195
196 #if 0 // not needed yet
197 /** updates the parse information with the given Info-ZIP Unix new extra field.
198 * @param buffer start of content of buffer known to contain an Info-ZIP
199 * Unix new extra field (without magic & size)
200 * @param size size of field content (must not count magic and size entries)
201 * @param islocal true if this is a local field, false if central
202 * @param pfi ParseFileInfo object to be updated
203 * @return true if processing was successful
204 */
205 static bool parseInfoZipUnixNew(const char *buffer, int size, bool islocal,
206 ParseFileInfo &pfi)
207 {
208 if (!islocal) { // contains nothing in central field
209 pfi.newinfounix = true;
210 return true;
211 }
212
213 if (size < 4) {
214 qCDebug(KArchiveLog) << "premature end of Info-ZIP unix extra field new";
215 return false;
216 }
217
218 pfi.uid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
219 buffer += 2;
220 pfi.gid = (uchar)buffer[0] | (uchar)buffer[1] << 8;
221 buffer += 2;
222
223 pfi.newinfounix = true;
224 return true;
225 }
226 #endif
227
228 /**
229 * parses the extra field
230 * @param buffer start of buffer where the extra field is to be found
231 * @param size size of the extra field
232 * @param islocal true if this is part of a local header, false if of central
233 * @param pfi ParseFileInfo object which to write the results into
234 * @return true if parsing was successful
235 */
parseExtraField(const char * buffer,int size,bool islocal,ParseFileInfo & pfi)236 static bool parseExtraField(const char *buffer, int size, bool islocal, ParseFileInfo &pfi)
237 {
238 // extra field in central directory doesn't contain useful data, so we
239 // don't bother parsing it
240 if (!islocal) {
241 return true;
242 }
243
244 while (size >= 4) { // as long as a potential extra field can be read
245 int magic = (uchar)buffer[0] | (uchar)buffer[1] << 8;
246 buffer += 2;
247 int fieldsize = (uchar)buffer[0] | (uchar)buffer[1] << 8;
248 buffer += 2;
249 size -= 4;
250
251 if (fieldsize > size) {
252 // qCDebug(KArchiveLog) << "fieldsize: " << fieldsize << " size: " << size;
253 // qCDebug(KArchiveLog) << "premature end of extra fields reached";
254 break;
255 }
256
257 switch (magic) {
258 case 0x5455: // extended timestamp
259 if (!parseExtTimestamp(buffer, fieldsize, islocal, pfi)) {
260 return false;
261 }
262 break;
263 case 0x5855: // old Info-ZIP unix extra field
264 if (!parseInfoZipUnixOld(buffer, fieldsize, islocal, pfi)) {
265 return false;
266 }
267 break;
268 #if 0 // not needed yet
269 case 0x7855: // new Info-ZIP unix extra field
270 if (!parseInfoZipUnixNew(buffer, fieldsize, islocal, pfi)) {
271 return false;
272 }
273 break;
274 #endif
275 default:
276 /* ignore everything else */
277 ;
278 } /*end switch*/
279
280 buffer += fieldsize;
281 size -= fieldsize;
282 } /*wend*/
283 return true;
284 }
285
286 /**
287 * Checks if a token for a central or local header has been found and resets
288 * the device to the begin of the token. If a token for the data descriptor is
289 * found it is assumed there is a central or local header token starting right
290 * behind the data descriptor, and the device is set accordingly to the begin
291 * of that token.
292 * To be called when a 'P' has been found.
293 * @param buffer start of buffer with the 3 bytes behind 'P'
294 * @param dev device that is read from
295 * @param dataDescriptor only search for data descriptor
296 * @return true if a local or central header begin is or could be reached
297 */
handlePossibleHeaderBegin(const char * buffer,QIODevice * dev,bool dataDescriptor)298 static bool handlePossibleHeaderBegin(const char *buffer, QIODevice *dev, bool dataDescriptor)
299 {
300 // we have to detect three magic tokens here:
301 // PK34 for the next local header in case there is no data descriptor
302 // PK12 for the central header in case there is no data descriptor
303 // PK78 for the data descriptor in case it is following the compressed data
304 // TODO: optimize using 32bit const data for comparison instead of byte-wise,
305 // given we run at least on 32bit CPUs
306
307 if (buffer[0] == 'K') {
308 if (buffer[1] == 7 && buffer[2] == 8) {
309 // data descriptor token found
310 dev->seek(dev->pos() + 12); // skip the 'data_descriptor'
311 return true;
312 }
313
314 if (!dataDescriptor
315 && ((buffer[1] == 1 && buffer[2] == 2) //
316 || (buffer[1] == 3 && buffer[2] == 4))) {
317 // central/local header token found
318 dev->seek(dev->pos() - 4);
319 // go back 4 bytes, so that the magic bytes can be found
320 // in the next cycle...
321 return true;
322 }
323 }
324 return false;
325 }
326
327 /**
328 * Reads the device forwards from the current pos until a token for a central or
329 * local header has been found or is to be assumed.
330 * @param dev device that is read from
331 * @return true if a local or central header token could be reached, false on error
332 */
seekToNextHeaderToken(QIODevice * dev,bool dataDescriptor)333 static bool seekToNextHeaderToken(QIODevice *dev, bool dataDescriptor)
334 {
335 bool headerTokenFound = false;
336 char buffer[3];
337
338 while (!headerTokenFound) {
339 int n = dev->read(buffer, 1);
340 if (n < 1) {
341 // qCWarning(KArchiveLog) << "Invalid ZIP file. Unexpected end of file. (#2)";
342 return false;
343 }
344
345 if (buffer[0] != 'P') {
346 continue;
347 }
348
349 n = dev->read(buffer, 3);
350 if (n < 3) {
351 // qCWarning(KArchiveLog) << "Invalid ZIP file. Unexpected end of file. (#3)";
352 return false;
353 }
354
355 if (handlePossibleHeaderBegin(buffer, dev, dataDescriptor)) {
356 headerTokenFound = true;
357 } else {
358 for (int i = 0; i < 3; ++i) {
359 if (buffer[i] == 'P') {
360 // We have another P character so we must go back a little to check if it is a magic
361 dev->seek(dev->pos() - 3 + i);
362 break;
363 }
364 }
365 }
366 }
367 return true;
368 }
369
370 ////////////////////////////////////////////////////////////////////////
371 /////////////////////////// KZip ///////////////////////////////////////
372 ////////////////////////////////////////////////////////////////////////
373
374 class Q_DECL_HIDDEN KZip::KZipPrivate
375 {
376 public:
KZipPrivate()377 KZipPrivate()
378 : m_crc(0)
379 , m_currentFile(nullptr)
380 , m_currentDev(nullptr)
381 , m_compression(8)
382 , m_extraField(KZip::NoExtraField)
383 , m_offset(0)
384 {
385 }
386
387 unsigned long m_crc; // checksum
388 KZipFileEntry *m_currentFile; // file currently being written
389 QIODevice *m_currentDev; // filterdev used to write to the above file
390 QList<KZipFileEntry *> m_fileList; // flat list of all files, for the index (saves a recursive method ;)
391 int m_compression;
392 KZip::ExtraField m_extraField;
393 // m_offset holds the offset of the place in the zip,
394 // where new data can be appended. after openarchive it points to 0, when in
395 // writeonly mode, or it points to the beginning of the central directory.
396 // each call to writefile updates this value.
397 quint64 m_offset;
398 };
399
KZip(const QString & fileName)400 KZip::KZip(const QString &fileName)
401 : KArchive(fileName)
402 , d(new KZipPrivate)
403 {
404 }
405
KZip(QIODevice * dev)406 KZip::KZip(QIODevice *dev)
407 : KArchive(dev)
408 , d(new KZipPrivate)
409 {
410 }
411
~KZip()412 KZip::~KZip()
413 {
414 // qCDebug(KArchiveLog) << this;
415 if (isOpen()) {
416 close();
417 }
418 delete d;
419 }
420
openArchive(QIODevice::OpenMode mode)421 bool KZip::openArchive(QIODevice::OpenMode mode)
422 {
423 // qCDebug(KArchiveLog);
424 d->m_fileList.clear();
425
426 if (mode == QIODevice::WriteOnly) {
427 return true;
428 }
429
430 char buffer[47];
431
432 // Check that it's a valid ZIP file
433 // KArchive::open() opened the underlying device already.
434
435 quint64 offset = 0; // holds offset, where we read
436 // contains information gathered from the local file headers
437 QHash<QByteArray, ParseFileInfo> pfi_map;
438
439 QIODevice *dev = device();
440
441 // We set a bool for knowing if we are allowed to skip the start of the file
442 bool startOfFile = true;
443
444 for (;;) { // repeat until 'end of entries' signature is reached
445 // qCDebug(KArchiveLog) << "loop starts";
446 // qCDebug(KArchiveLog) << "dev->pos() now : " << dev->pos();
447 int n = dev->read(buffer, 4);
448
449 if (n < 4) {
450 setErrorString(tr("Invalid ZIP file. Unexpected end of file. (Error code: %1)").arg(1));
451 return false;
452 }
453
454 if (!memcmp(buffer, "PK\5\6", 4)) { // 'end of entries'
455 // qCDebug(KArchiveLog) << "PK56 found end of archive";
456 startOfFile = false;
457 break;
458 }
459
460 if (!memcmp(buffer, "PK\3\4", 4)) { // local file header
461 // qCDebug(KArchiveLog) << "PK34 found local file header";
462 startOfFile = false;
463 // can this fail ???
464 dev->seek(dev->pos() + 2); // skip 'version needed to extract'
465
466 // read static header stuff
467 n = dev->read(buffer, 24);
468 if (n < 24) {
469 setErrorString(tr("Invalid ZIP file. Unexpected end of file. (Error code: %1)").arg(4));
470 return false;
471 }
472
473 int gpf = (uchar)buffer[0]; // "general purpose flag" not "general protection fault" ;-)
474 int compression_mode = (uchar)buffer[2] | (uchar)buffer[3] << 8;
475 uint mtime = transformFromMsDos(buffer + 4);
476
477 const qint64 compr_size = uint(uchar(buffer[12])) | uint(uchar(buffer[13])) << 8 | uint(uchar(buffer[14])) << 16 | uint(uchar(buffer[15])) << 24;
478 const qint64 uncomp_size = uint(uchar(buffer[16])) | uint(uchar(buffer[17])) << 8 | uint(uchar(buffer[18])) << 16 | uint(uchar(buffer[19])) << 24;
479 const int namelen = uint(uchar(buffer[20])) | uint(uchar(buffer[21])) << 8;
480 const int extralen = uint(uchar(buffer[22])) | uint(uchar(buffer[23])) << 8;
481
482 /*
483 qCDebug(KArchiveLog) << "general purpose bit flag: " << gpf;
484 qCDebug(KArchiveLog) << "compressed size: " << compr_size;
485 qCDebug(KArchiveLog) << "uncompressed size: " << uncomp_size;
486 qCDebug(KArchiveLog) << "namelen: " << namelen;
487 qCDebug(KArchiveLog) << "extralen: " << extralen;
488 qCDebug(KArchiveLog) << "archive size: " << dev->size();
489 */
490
491 // read fileName
492 if (namelen <= 0) {
493 setErrorString(tr("Invalid ZIP file. Negative name length"));
494 return false;
495 }
496 QByteArray fileName = dev->read(namelen);
497 if (fileName.size() < namelen) {
498 setErrorString(tr("Invalid ZIP file. Name not completely read (#2)"));
499 return false;
500 }
501
502 ParseFileInfo pfi;
503 pfi.mtime = mtime;
504
505 // read and parse the beginning of the extra field,
506 // skip rest of extra field in case it is too long
507 unsigned int extraFieldEnd = dev->pos() + extralen;
508 pfi.extralen = extralen;
509 int handledextralen = qMin(extralen, (int)sizeof buffer);
510
511 // if (handledextralen)
512 // qCDebug(KArchiveLog) << "handledextralen: " << handledextralen;
513
514 n = dev->read(buffer, handledextralen);
515 // no error msg necessary as we deliberately truncate the extra field
516 if (!parseExtraField(buffer, n, true, pfi)) {
517 setErrorString(tr("Invalid ZIP File. Broken ExtraField."));
518 return false;
519 }
520
521 // jump to end of extra field
522 dev->seek(extraFieldEnd);
523
524 // we have to take care of the 'general purpose bit flag'.
525 // if bit 3 is set, the header doesn't contain the length of
526 // the file and we look for the signature 'PK\7\8'.
527 if (gpf & 8) {
528 // here we have to read through the compressed data to find
529 // the next PKxx
530 if (!seekToNextHeaderToken(dev, true)) {
531 setErrorString(tr("Could not seek to next header token"));
532 return false;
533 }
534 } else {
535 // here we skip the compressed data and jump to the next header
536 // qCDebug(KArchiveLog) << "general purpose bit flag indicates, that local file header contains valid size";
537 bool foundSignature = false;
538 // check if this could be a symbolic link
539 if (compression_mode == NoCompression //
540 && uncomp_size <= max_path_len //
541 && uncomp_size > 0) {
542 // read content and store it
543 // If it's not a symlink, then we'll just discard the data for now.
544 pfi.guessed_symlink = dev->read(uncomp_size);
545 if (pfi.guessed_symlink.size() < uncomp_size) {
546 setErrorString(tr("Invalid ZIP file. Unexpected end of file. (#5)"));
547 return false;
548 }
549 } else {
550 if (compr_size > dev->size()) {
551 // here we cannot trust the compressed size, so scan through the compressed
552 // data to find the next header
553 if (!seekToNextHeaderToken(dev, false)) {
554 setErrorString(tr("Could not seek to next header token"));
555 return false;
556 }
557 foundSignature = true;
558 } else {
559 // qCDebug(KArchiveLog) << "before interesting dev->pos(): " << dev->pos();
560 const bool success = dev->seek(dev->pos() + compr_size);
561 if (!success) {
562 setErrorString(tr("Could not seek to file compressed size"));
563 return false;
564 }
565 /* qCDebug(KArchiveLog) << "after interesting dev->pos(): " << dev->pos();
566 if (success)
567 qCDebug(KArchiveLog) << "dev->at was successful... ";
568 else
569 qCDebug(KArchiveLog) << "dev->at failed... ";*/
570 }
571 }
572 // test for optional data descriptor
573 if (!foundSignature) {
574 // qCDebug(KArchiveLog) << "Testing for optional data descriptor";
575 // read static data descriptor
576 n = dev->read(buffer, 4);
577 if (n < 4) {
578 setErrorString(tr("Invalid ZIP file. Unexpected end of file. (#1)"));
579 return false;
580 }
581
582 if (buffer[0] != 'P' || !handlePossibleHeaderBegin(buffer + 1, dev, false)) {
583 // assume data descriptor without signature
584 dev->seek(dev->pos() + 8); // skip rest of the 'data_descriptor'
585 }
586 }
587
588 // not needed any more
589 /* // here we calculate the length of the file in the zip
590 // with headers and jump to the next header.
591 uint skip = compr_size + namelen + extralen;
592 offset += 30 + skip;*/
593 }
594 pfi_map.insert(fileName, pfi);
595 } else if (!memcmp(buffer, "PK\1\2", 4)) { // central block
596 // qCDebug(KArchiveLog) << "PK12 found central block";
597 startOfFile = false;
598
599 // so we reached the central header at the end of the zip file
600 // here we get all interesting data out of the central header
601 // of a file
602 offset = dev->pos() - 4;
603
604 // set offset for appending new files
605 if (d->m_offset == 0) {
606 d->m_offset = offset;
607 }
608
609 n = dev->read(buffer + 4, 42);
610 if (n < 42) {
611 setErrorString(tr("Invalid ZIP file, central entry too short (not long enough for valid entry)"));
612 return false;
613 }
614
615 // int gpf = (uchar)buffer[9] << 8 | (uchar)buffer[10];
616 // qCDebug(KArchiveLog) << "general purpose flag=" << gpf;
617 // length of the fileName (well, pathname indeed)
618 int namelen = (uchar)buffer[29] << 8 | (uchar)buffer[28];
619 if (namelen <= 0) {
620 setErrorString(tr("Invalid ZIP file, file path name length smaller or equal to zero"));
621 return false;
622 }
623 QByteArray bufferName = dev->read(namelen);
624 if (bufferName.size() < namelen) {
625 // qCWarning(KArchiveLog) << "Invalid ZIP file. Name not completely read";
626 }
627
628 ParseFileInfo pfi = pfi_map.value(bufferName, ParseFileInfo());
629
630 QString name(QFile::decodeName(bufferName));
631
632 // qCDebug(KArchiveLog) << "name: " << name;
633 // only in central header ! see below.
634 // length of extra attributes
635 int extralen = (uchar)buffer[31] << 8 | (uchar)buffer[30];
636 // length of comment for this file
637 int commlen = (uchar)buffer[33] << 8 | (uchar)buffer[32];
638 // compression method of this file
639 int cmethod = (uchar)buffer[11] << 8 | (uchar)buffer[10];
640
641 // qCDebug(KArchiveLog) << "cmethod: " << cmethod;
642 // qCDebug(KArchiveLog) << "extralen: " << extralen;
643
644 // crc32 of the file
645 uint crc32 = (uchar)buffer[19] << 24 | (uchar)buffer[18] << 16 | (uchar)buffer[17] << 8 | (uchar)buffer[16];
646
647 // uncompressed file size
648 uint ucsize = (uchar)buffer[27] << 24 | (uchar)buffer[26] << 16 | (uchar)buffer[25] << 8 | (uchar)buffer[24];
649 // compressed file size
650 uint csize = (uchar)buffer[23] << 24 | (uchar)buffer[22] << 16 | (uchar)buffer[21] << 8 | (uchar)buffer[20];
651
652 // offset of local header
653 uint localheaderoffset = (uchar)buffer[45] << 24 | (uchar)buffer[44] << 16 | (uchar)buffer[43] << 8 | (uchar)buffer[42];
654
655 // some clever people use different extra field lengths
656 // in the central header and in the local header... funny.
657 // so we need to get the localextralen to calculate the offset
658 // from localheaderstart to dataoffset
659 int localextralen = pfi.extralen; // FIXME: this will not work if
660 // no local header exists
661
662 // qCDebug(KArchiveLog) << "localextralen: " << localextralen;
663
664 // offset, where the real data for uncompression starts
665 uint dataoffset = localheaderoffset + 30 + localextralen + namelen; // comment only in central header
666
667 // qCDebug(KArchiveLog) << "csize: " << csize;
668
669 int os_madeby = (uchar)buffer[5];
670 bool isdir = false;
671 int access = 0100644;
672
673 if (os_madeby == 3) { // good ole unix
674 access = (uchar)buffer[40] | (uchar)buffer[41] << 8;
675 }
676
677 QString entryName;
678
679 if (name.endsWith(QLatin1Char('/'))) { // Entries with a trailing slash are directories
680 isdir = true;
681 name = name.left(name.length() - 1);
682 if (os_madeby != 3) {
683 access = S_IFDIR | 0755;
684 } else {
685 access |= S_IFDIR | 0700;
686 }
687 }
688
689 int pos = name.lastIndexOf(QLatin1Char('/'));
690 if (pos == -1) {
691 entryName = name;
692 } else {
693 entryName = name.mid(pos + 1);
694 }
695 if (entryName.isEmpty()) {
696 setErrorString(tr("Invalid ZIP file, found empty entry name"));
697 return false;
698 }
699
700 KArchiveEntry *entry;
701 if (isdir) {
702 QString path = QDir::cleanPath(name);
703 const KArchiveEntry *ent = rootDir()->entry(path);
704 if (ent && ent->isDirectory()) {
705 // qCDebug(KArchiveLog) << "Directory already exists, NOT going to add it again";
706 entry = nullptr;
707 } else {
708 QDateTime mtime = KArchivePrivate::time_tToDateTime(pfi.mtime);
709 entry = new KArchiveDirectory(this, entryName, access, mtime, rootDir()->user(), rootDir()->group(), QString());
710 // qCDebug(KArchiveLog) << "KArchiveDirectory created, entryName= " << entryName << ", name=" << name;
711 }
712 } else {
713 QString symlink;
714 if ((access & QT_STAT_MASK) == QT_STAT_LNK) {
715 symlink = QFile::decodeName(pfi.guessed_symlink);
716 }
717 QDateTime mtime = KArchivePrivate::time_tToDateTime(pfi.mtime);
718 entry =
719 new KZipFileEntry(this, entryName, access, mtime, rootDir()->user(), rootDir()->group(), symlink, name, dataoffset, ucsize, cmethod, csize);
720 static_cast<KZipFileEntry *>(entry)->setHeaderStart(localheaderoffset);
721 static_cast<KZipFileEntry *>(entry)->setCRC32(crc32);
722 // qCDebug(KArchiveLog) << "KZipFileEntry created, entryName= " << entryName << ", name=" << name;
723 d->m_fileList.append(static_cast<KZipFileEntry *>(entry));
724 }
725
726 if (entry) {
727 if (pos == -1) {
728 rootDir()->addEntry(entry);
729 } else {
730 // In some tar files we can find dir/./file => call cleanPath
731 QString path = QDir::cleanPath(name.left(pos));
732 // Ensure container directory exists, create otherwise
733 KArchiveDirectory *tdir = findOrCreate(path);
734 if (tdir) {
735 tdir->addEntry(entry);
736 } else {
737 setErrorString(tr("File %1 is in folder %2, but %3 is actually a file.").arg(entryName, path, path));
738 delete entry;
739 return false;
740 }
741 }
742 }
743
744 // calculate offset to next entry
745 offset += 46 + commlen + extralen + namelen;
746 const bool b = dev->seek(offset);
747 if (!b) {
748 setErrorString(tr("Could not seek to next entry"));
749 return false;
750 }
751 } else if (startOfFile) {
752 // The file does not start with any ZIP header (e.g. self-extractable ZIP files)
753 // Therefore we need to find the first PK\003\004 (local header)
754 // qCDebug(KArchiveLog) << "Try to skip start of file";
755 startOfFile = false;
756 bool foundSignature = false;
757
758 while (!foundSignature) {
759 n = dev->read(buffer, 1);
760 if (n < 1) {
761 setErrorString(tr("Invalid ZIP file. Unexpected end of file."));
762 return false;
763 }
764
765 if (buffer[0] != 'P') {
766 continue;
767 }
768
769 n = dev->read(buffer, 3);
770 if (n < 3) {
771 setErrorString(tr("Invalid ZIP file. Unexpected end of file."));
772 return false;
773 }
774
775 // We have to detect the magic token for a local header: PK\003\004
776 /*
777 * Note: we do not need to check the other magics, if the ZIP file has no
778 * local header, then it has not any files!
779 */
780 if (buffer[0] == 'K' && buffer[1] == 3 && buffer[2] == 4) {
781 foundSignature = true;
782 dev->seek(dev->pos() - 4); // go back 4 bytes, so that the magic bytes can be found...
783 } else {
784 for (int i = 0; i < 3; ++i) {
785 if (buffer[i] == 'P') {
786 // We have another P character so we must go back a little to check if it is a magic
787 dev->seek(dev->pos() - 3 + i);
788 break;
789 }
790 }
791 }
792 }
793 } else {
794 setErrorString(tr("Invalid ZIP file. Unrecognized header at offset %1").arg(dev->pos() - 4));
795 return false;
796 }
797 }
798 // qCDebug(KArchiveLog) << "*** done *** ";
799 return true;
800 }
801
closeArchive()802 bool KZip::closeArchive()
803 {
804 if (!(mode() & QIODevice::WriteOnly)) {
805 // qCDebug(KArchiveLog) << "readonly";
806 return true;
807 }
808
809 // ReadWrite or WriteOnly
810 // write all central dir file entries
811
812 // to be written at the end of the file...
813 char buffer[22]; // first used for 12, then for 22 at the end
814 uLong crc = crc32(0L, nullptr, 0);
815
816 qint64 centraldiroffset = device()->pos();
817 // qCDebug(KArchiveLog) << "closearchive: centraldiroffset: " << centraldiroffset;
818 qint64 atbackup = centraldiroffset;
819 QMutableListIterator<KZipFileEntry *> it(d->m_fileList);
820
821 while (it.hasNext()) {
822 // set crc and compressed size in each local file header
823 it.next();
824 if (!device()->seek(it.value()->headerStart() + 14)) {
825 setErrorString(tr("Could not seek to next file header: %1").arg(device()->errorString()));
826 return false;
827 }
828 // qCDebug(KArchiveLog) << "closearchive setcrcandcsize: fileName:"
829 // << it.value()->path()
830 // << "encoding:" << it.value()->encoding();
831
832 uLong mycrc = it.value()->crc32();
833 buffer[0] = char(mycrc); // crc checksum, at headerStart+14
834 buffer[1] = char(mycrc >> 8);
835 buffer[2] = char(mycrc >> 16);
836 buffer[3] = char(mycrc >> 24);
837
838 int mysize1 = it.value()->compressedSize();
839 buffer[4] = char(mysize1); // compressed file size, at headerStart+18
840 buffer[5] = char(mysize1 >> 8);
841 buffer[6] = char(mysize1 >> 16);
842 buffer[7] = char(mysize1 >> 24);
843
844 int myusize = it.value()->size();
845 buffer[8] = char(myusize); // uncompressed file size, at headerStart+22
846 buffer[9] = char(myusize >> 8);
847 buffer[10] = char(myusize >> 16);
848 buffer[11] = char(myusize >> 24);
849
850 if (device()->write(buffer, 12) != 12) {
851 setErrorString(tr("Could not write file header: %1").arg(device()->errorString()));
852 return false;
853 }
854 }
855 device()->seek(atbackup);
856
857 it.toFront();
858 while (it.hasNext()) {
859 it.next();
860 // qCDebug(KArchiveLog) << "fileName:" << it.value()->path()
861 // << "encoding:" << it.value()->encoding();
862
863 QByteArray path = QFile::encodeName(it.value()->path());
864
865 const int extra_field_len = (d->m_extraField == ModificationTime) ? 9 : 0;
866 const int bufferSize = extra_field_len + path.length() + 46;
867 char *buffer = new char[bufferSize];
868
869 memset(buffer, 0, 46); // zero is a nice default for most header fields
870
871 /* clang-format off */
872 const char head[] = {
873 'P', 'K', 1, 2, // central file header signature
874 0x14, 3, // version made by (3 == UNIX)
875 0x14, 0 // version needed to extract
876 };
877 /* clang-format on */
878
879 // I do not know why memcpy is not working here
880 // memcpy(buffer, head, sizeof(head));
881 memmove(buffer, head, sizeof(head));
882
883 buffer[10] = char(it.value()->encoding()); // compression method
884 buffer[11] = char(it.value()->encoding() >> 8);
885
886 transformToMsDos(it.value()->date(), &buffer[12]);
887
888 uLong mycrc = it.value()->crc32();
889 buffer[16] = char(mycrc); // crc checksum
890 buffer[17] = char(mycrc >> 8);
891 buffer[18] = char(mycrc >> 16);
892 buffer[19] = char(mycrc >> 24);
893
894 int mysize1 = it.value()->compressedSize();
895 buffer[20] = char(mysize1); // compressed file size
896 buffer[21] = char(mysize1 >> 8);
897 buffer[22] = char(mysize1 >> 16);
898 buffer[23] = char(mysize1 >> 24);
899
900 int mysize = it.value()->size();
901 buffer[24] = char(mysize); // uncompressed file size
902 buffer[25] = char(mysize >> 8);
903 buffer[26] = char(mysize >> 16);
904 buffer[27] = char(mysize >> 24);
905
906 buffer[28] = char(path.length()); // fileName length
907 buffer[29] = char(path.length() >> 8);
908
909 buffer[30] = char(extra_field_len);
910 buffer[31] = char(extra_field_len >> 8);
911
912 buffer[40] = char(it.value()->permissions());
913 buffer[41] = char(it.value()->permissions() >> 8);
914
915 int myhst = it.value()->headerStart();
916 buffer[42] = char(myhst); // relative offset of local header
917 buffer[43] = char(myhst >> 8);
918 buffer[44] = char(myhst >> 16);
919 buffer[45] = char(myhst >> 24);
920
921 // file name
922 strncpy(buffer + 46, path.constData(), path.length());
923 // qCDebug(KArchiveLog) << "closearchive length to write: " << bufferSize;
924
925 // extra field
926 if (d->m_extraField == ModificationTime) {
927 char *extfield = buffer + 46 + path.length();
928 // "Extended timestamp" header (0x5455)
929 extfield[0] = 'U';
930 extfield[1] = 'T';
931 extfield[2] = 5; // data size
932 extfield[3] = 0;
933 extfield[4] = 1 | 2 | 4; // specify flags from local field
934 // (unless I misread the spec)
935 // provide only modification time
936 unsigned long time = (unsigned long)it.value()->date().toSecsSinceEpoch();
937 extfield[5] = char(time);
938 extfield[6] = char(time >> 8);
939 extfield[7] = char(time >> 16);
940 extfield[8] = char(time >> 24);
941 }
942
943 crc = crc32(crc, (Bytef *)buffer, bufferSize);
944 bool ok = (device()->write(buffer, bufferSize) == bufferSize);
945 delete[] buffer;
946 if (!ok) {
947 setErrorString(tr("Could not write file header: %1").arg(device()->errorString()));
948 return false;
949 }
950 }
951 qint64 centraldirendoffset = device()->pos();
952 // qCDebug(KArchiveLog) << "closearchive: centraldirendoffset: " << centraldirendoffset;
953 // qCDebug(KArchiveLog) << "closearchive: device()->pos(): " << device()->pos();
954
955 // write end of central dir record.
956 buffer[0] = 'P'; // end of central dir signature
957 buffer[1] = 'K';
958 buffer[2] = 5;
959 buffer[3] = 6;
960
961 buffer[4] = 0; // number of this disk
962 buffer[5] = 0;
963
964 buffer[6] = 0; // number of disk with start of central dir
965 buffer[7] = 0;
966
967 int count = d->m_fileList.count();
968 // qCDebug(KArchiveLog) << "number of files (count): " << count;
969
970 buffer[8] = char(count); // total number of entries in central dir of
971 buffer[9] = char(count >> 8); // this disk
972
973 buffer[10] = buffer[8]; // total number of entries in the central dir
974 buffer[11] = buffer[9];
975
976 int cdsize = centraldirendoffset - centraldiroffset;
977 buffer[12] = char(cdsize); // size of the central dir
978 buffer[13] = char(cdsize >> 8);
979 buffer[14] = char(cdsize >> 16);
980 buffer[15] = char(cdsize >> 24);
981
982 // qCDebug(KArchiveLog) << "end : centraldiroffset: " << centraldiroffset;
983 // qCDebug(KArchiveLog) << "end : centraldirsize: " << cdsize;
984
985 buffer[16] = char(centraldiroffset); // central dir offset
986 buffer[17] = char(centraldiroffset >> 8);
987 buffer[18] = char(centraldiroffset >> 16);
988 buffer[19] = char(centraldiroffset >> 24);
989
990 buffer[20] = 0; // zipfile comment length
991 buffer[21] = 0;
992
993 if (device()->write(buffer, 22) != 22) {
994 setErrorString(tr("Could not write central dir record: %1").arg(device()->errorString()));
995 return false;
996 }
997
998 return true;
999 }
1000
doWriteDir(const QString & name,const QString & user,const QString & group,mode_t perm,const QDateTime & atime,const QDateTime & mtime,const QDateTime & ctime)1001 bool KZip::doWriteDir(const QString &name,
1002 const QString &user,
1003 const QString &group,
1004 mode_t perm,
1005 const QDateTime &atime,
1006 const QDateTime &mtime,
1007 const QDateTime &ctime)
1008 {
1009 // Zip files have no explicit directories, they are implicitly created during extraction time
1010 // when file entries have paths in them.
1011 // However, to support empty directories, we must create a dummy file entry which ends with '/'.
1012 QString dirName = name;
1013 if (!name.endsWith(QLatin1Char('/'))) {
1014 dirName = dirName.append(QLatin1Char('/'));
1015 }
1016 return writeFile(dirName, QByteArray(), perm, user, group, atime, mtime, ctime);
1017 }
1018
doPrepareWriting(const QString & name,const QString & user,const QString & group,qint64,mode_t perm,const QDateTime & accessTime,const QDateTime & modificationTime,const QDateTime & creationTime)1019 bool KZip::doPrepareWriting(const QString &name,
1020 const QString &user,
1021 const QString &group,
1022 qint64 /*size*/,
1023 mode_t perm,
1024 const QDateTime &accessTime,
1025 const QDateTime &modificationTime,
1026 const QDateTime &creationTime)
1027 {
1028 // qCDebug(KArchiveLog);
1029 if (!isOpen()) {
1030 setErrorString(tr("Application error: ZIP file must be open before being written into"));
1031 qCWarning(KArchiveLog) << "doPrepareWriting failed: !isOpen()";
1032 return false;
1033 }
1034
1035 if (!(mode() & QIODevice::WriteOnly)) { // accept WriteOnly and ReadWrite
1036 setErrorString(tr("Application error: attempted to write into non-writable ZIP file"));
1037 qCWarning(KArchiveLog) << "doPrepareWriting failed: !(mode() & QIODevice::WriteOnly)";
1038 return false;
1039 }
1040
1041 if (!device()) {
1042 setErrorString(tr("Cannot create a device. Disk full?"));
1043 return false;
1044 }
1045
1046 // set right offset in zip.
1047 if (!device()->seek(d->m_offset)) {
1048 setErrorString(tr("Cannot seek in ZIP file. Disk full?"));
1049 return false;
1050 }
1051
1052 uint atime = accessTime.toSecsSinceEpoch();
1053 uint mtime = modificationTime.toSecsSinceEpoch();
1054 uint ctime = creationTime.toSecsSinceEpoch();
1055
1056 // Find or create parent dir
1057 KArchiveDirectory *parentDir = rootDir();
1058 QString fileName(name);
1059 int i = name.lastIndexOf(QLatin1Char('/'));
1060 if (i != -1) {
1061 QString dir = name.left(i);
1062 fileName = name.mid(i + 1);
1063 // qCDebug(KArchiveLog) << "ensuring" << dir << "exists. fileName=" << fileName;
1064 parentDir = findOrCreate(dir);
1065 }
1066
1067 // delete entries in the filelist with the same fileName as the one we want
1068 // to save, so that we don't have duplicate file entries when viewing the zip
1069 // with konqi...
1070 // CAUTION: the old file itself is still in the zip and won't be removed !!!
1071 QMutableListIterator<KZipFileEntry *> it(d->m_fileList);
1072 // qCDebug(KArchiveLog) << "fileName to write: " << name;
1073 while (it.hasNext()) {
1074 it.next();
1075 // qCDebug(KArchiveLog) << "prepfileName: " << it.value()->path();
1076 if (name == it.value()->path()) {
1077 // also remove from the parentDir
1078 parentDir->removeEntry(it.value());
1079 // qCDebug(KArchiveLog) << "removing following entry: " << it.value()->path();
1080 delete it.value();
1081 it.remove();
1082 }
1083 }
1084
1085 // construct a KZipFileEntry and add it to list
1086 KZipFileEntry *e = new KZipFileEntry(this,
1087 fileName,
1088 perm,
1089 modificationTime,
1090 user,
1091 group,
1092 QString(),
1093 name,
1094 device()->pos() + 30 + name.length(), // start
1095 0 /*size unknown yet*/,
1096 d->m_compression,
1097 0 /*csize unknown yet*/);
1098 e->setHeaderStart(device()->pos());
1099 // qCDebug(KArchiveLog) << "wrote file start: " << e->position() << " name: " << name;
1100 if (!parentDir->addEntryV2(e)) {
1101 return false;
1102 }
1103
1104 d->m_currentFile = e;
1105 d->m_fileList.append(e);
1106
1107 int extra_field_len = 0;
1108 if (d->m_extraField == ModificationTime) {
1109 extra_field_len = 17; // value also used in finishWriting()
1110 }
1111
1112 // write out zip header
1113 QByteArray encodedName = QFile::encodeName(name);
1114 int bufferSize = extra_field_len + encodedName.length() + 30;
1115 // qCDebug(KArchiveLog) << "bufferSize=" << bufferSize;
1116 char *buffer = new char[bufferSize];
1117
1118 buffer[0] = 'P'; // local file header signature
1119 buffer[1] = 'K';
1120 buffer[2] = 3;
1121 buffer[3] = 4;
1122
1123 buffer[4] = 0x14; // version needed to extract
1124 buffer[5] = 0;
1125
1126 buffer[6] = 0; // general purpose bit flag
1127 buffer[7] = 0;
1128
1129 buffer[8] = char(e->encoding()); // compression method
1130 buffer[9] = char(e->encoding() >> 8);
1131
1132 transformToMsDos(e->date(), &buffer[10]);
1133
1134 buffer[14] = 'C'; // dummy crc
1135 buffer[15] = 'R';
1136 buffer[16] = 'C';
1137 buffer[17] = 'q';
1138
1139 buffer[18] = 'C'; // compressed file size
1140 buffer[19] = 'S';
1141 buffer[20] = 'I';
1142 buffer[21] = 'Z';
1143
1144 buffer[22] = 'U'; // uncompressed file size
1145 buffer[23] = 'S';
1146 buffer[24] = 'I';
1147 buffer[25] = 'Z';
1148
1149 buffer[26] = (uchar)(encodedName.length()); // fileName length
1150 buffer[27] = (uchar)(encodedName.length() >> 8);
1151
1152 buffer[28] = (uchar)(extra_field_len); // extra field length
1153 buffer[29] = (uchar)(extra_field_len >> 8);
1154
1155 // file name
1156 strncpy(buffer + 30, encodedName.constData(), encodedName.length());
1157
1158 // extra field
1159 if (d->m_extraField == ModificationTime) {
1160 char *extfield = buffer + 30 + encodedName.length();
1161 // "Extended timestamp" header (0x5455)
1162 extfield[0] = 'U';
1163 extfield[1] = 'T';
1164 extfield[2] = 13; // data size
1165 extfield[3] = 0;
1166 extfield[4] = 1 | 2 | 4; // contains mtime, atime, ctime
1167
1168 extfield[5] = char(mtime);
1169 extfield[6] = char(mtime >> 8);
1170 extfield[7] = char(mtime >> 16);
1171 extfield[8] = char(mtime >> 24);
1172
1173 extfield[9] = char(atime);
1174 extfield[10] = char(atime >> 8);
1175 extfield[11] = char(atime >> 16);
1176 extfield[12] = char(atime >> 24);
1177
1178 extfield[13] = char(ctime);
1179 extfield[14] = char(ctime >> 8);
1180 extfield[15] = char(ctime >> 16);
1181 extfield[16] = char(ctime >> 24);
1182 }
1183
1184 // Write header
1185 bool b = (device()->write(buffer, bufferSize) == bufferSize);
1186 d->m_crc = 0;
1187 delete[] buffer;
1188
1189 if (!b) {
1190 setErrorString(tr("Could not write to the archive. Disk full?"));
1191 return false;
1192 }
1193
1194 // Prepare device for writing the data
1195 // Either device() if no compression, or a KCompressionDevice to compress
1196 if (d->m_compression == 0) {
1197 d->m_currentDev = device();
1198 return true;
1199 }
1200
1201 auto compressionDevice = new KCompressionDevice(device(), false, KCompressionDevice::GZip);
1202 d->m_currentDev = compressionDevice;
1203 compressionDevice->setSkipHeaders(); // Just zlib, not gzip
1204
1205 b = d->m_currentDev->open(QIODevice::WriteOnly);
1206 Q_ASSERT(b);
1207
1208 if (!b) {
1209 setErrorString(tr("Could not open compression device: %1").arg(d->m_currentDev->errorString()));
1210 }
1211
1212 return b;
1213 }
1214
doFinishWriting(qint64 size)1215 bool KZip::doFinishWriting(qint64 size)
1216 {
1217 if (d->m_currentFile->encoding() == 8) {
1218 // Finish
1219 (void)d->m_currentDev->write(nullptr, 0);
1220 delete d->m_currentDev;
1221 }
1222 // If 0, d->m_currentDev was device() - don't delete ;)
1223 d->m_currentDev = nullptr;
1224
1225 Q_ASSERT(d->m_currentFile);
1226 // qCDebug(KArchiveLog) << "fileName: " << d->m_currentFile->path();
1227 // qCDebug(KArchiveLog) << "getpos (at): " << device()->pos();
1228 d->m_currentFile->setSize(size);
1229 int extra_field_len = 0;
1230 if (d->m_extraField == ModificationTime) {
1231 extra_field_len = 17; // value also used in finishWriting()
1232 }
1233
1234 const QByteArray encodedName = QFile::encodeName(d->m_currentFile->path());
1235 int csize = device()->pos() - d->m_currentFile->headerStart() - 30 - encodedName.length() - extra_field_len;
1236 d->m_currentFile->setCompressedSize(csize);
1237 // qCDebug(KArchiveLog) << "usize: " << d->m_currentFile->size();
1238 // qCDebug(KArchiveLog) << "csize: " << d->m_currentFile->compressedSize();
1239 // qCDebug(KArchiveLog) << "headerstart: " << d->m_currentFile->headerStart();
1240
1241 // qCDebug(KArchiveLog) << "crc: " << d->m_crc;
1242 d->m_currentFile->setCRC32(d->m_crc);
1243
1244 d->m_currentFile = nullptr;
1245
1246 // update saved offset for appending new files
1247 d->m_offset = device()->pos();
1248 return true;
1249 }
1250
doWriteSymLink(const QString & name,const QString & target,const QString & user,const QString & group,mode_t perm,const QDateTime & atime,const QDateTime & mtime,const QDateTime & ctime)1251 bool KZip::doWriteSymLink(const QString &name,
1252 const QString &target,
1253 const QString &user,
1254 const QString &group,
1255 mode_t perm,
1256 const QDateTime &atime,
1257 const QDateTime &mtime,
1258 const QDateTime &ctime)
1259 {
1260 // reassure that symlink flag is set, otherwise strange things happen on
1261 // extraction
1262 perm |= QT_STAT_LNK;
1263 Compression c = compression();
1264 setCompression(NoCompression); // link targets are never compressed
1265
1266 if (!doPrepareWriting(name, user, group, 0, perm, atime, mtime, ctime)) {
1267 setCompression(c);
1268 return false;
1269 }
1270
1271 QByteArray symlink_target = QFile::encodeName(target);
1272 if (!writeData(symlink_target.constData(), symlink_target.length())) {
1273 setCompression(c);
1274 return false;
1275 }
1276
1277 if (!finishWriting(symlink_target.length())) {
1278 setCompression(c);
1279 return false;
1280 }
1281
1282 setCompression(c);
1283 return true;
1284 }
1285
virtual_hook(int id,void * data)1286 void KZip::virtual_hook(int id, void *data)
1287 {
1288 KArchive::virtual_hook(id, data);
1289 }
1290
writeData(const char * data,qint64 size)1291 bool KZip::writeData(const char *data, qint64 size)
1292 {
1293 Q_ASSERT(d->m_currentFile);
1294 Q_ASSERT(d->m_currentDev);
1295 if (!d->m_currentFile || !d->m_currentDev) {
1296 setErrorString(tr("No file or device"));
1297 return false;
1298 }
1299
1300 // crc to be calculated over uncompressed stuff...
1301 // and they didn't mention it in their docs...
1302 d->m_crc = crc32(d->m_crc, (const Bytef *)data, size);
1303
1304 qint64 written = d->m_currentDev->write(data, size);
1305 // qCDebug(KArchiveLog) << "wrote" << size << "bytes.";
1306 const bool ok = written == size;
1307
1308 if (!ok) {
1309 setErrorString(tr("Error writing data: %1").arg(d->m_currentDev->errorString()));
1310 }
1311
1312 return ok;
1313 }
1314
setCompression(Compression c)1315 void KZip::setCompression(Compression c)
1316 {
1317 d->m_compression = (c == NoCompression) ? 0 : 8;
1318 }
1319
compression() const1320 KZip::Compression KZip::compression() const
1321 {
1322 return (d->m_compression == 8) ? DeflateCompression : NoCompression;
1323 }
1324
setExtraField(ExtraField ef)1325 void KZip::setExtraField(ExtraField ef)
1326 {
1327 d->m_extraField = ef;
1328 }
1329
extraField() const1330 KZip::ExtraField KZip::extraField() const
1331 {
1332 return d->m_extraField;
1333 }
1334
1335 ////////////////////////////////////////////////////////////////////////
1336 ////////////////////// KZipFileEntry////////////////////////////////////
1337 ////////////////////////////////////////////////////////////////////////
1338 class Q_DECL_HIDDEN KZipFileEntry::KZipFileEntryPrivate
1339 {
1340 public:
KZipFileEntryPrivate()1341 KZipFileEntryPrivate()
1342 : crc(0)
1343 , compressedSize(0)
1344 , headerStart(0)
1345 , encoding(0)
1346 {
1347 }
1348 unsigned long crc;
1349 qint64 compressedSize;
1350 qint64 headerStart;
1351 int encoding;
1352 QString path;
1353 };
1354
KZipFileEntry(KZip * zip,const QString & name,int access,const QDateTime & date,const QString & user,const QString & group,const QString & symlink,const QString & path,qint64 start,qint64 uncompressedSize,int encoding,qint64 compressedSize)1355 KZipFileEntry::KZipFileEntry(KZip *zip,
1356 const QString &name,
1357 int access,
1358 const QDateTime &date,
1359 const QString &user,
1360 const QString &group,
1361 const QString &symlink,
1362 const QString &path,
1363 qint64 start,
1364 qint64 uncompressedSize,
1365 int encoding,
1366 qint64 compressedSize)
1367 : KArchiveFile(zip, name, access, date, user, group, symlink, start, uncompressedSize)
1368 , d(new KZipFileEntryPrivate)
1369 {
1370 d->path = path;
1371 d->encoding = encoding;
1372 d->compressedSize = compressedSize;
1373 }
1374
~KZipFileEntry()1375 KZipFileEntry::~KZipFileEntry()
1376 {
1377 delete d;
1378 }
1379
encoding() const1380 int KZipFileEntry::encoding() const
1381 {
1382 return d->encoding;
1383 }
1384
compressedSize() const1385 qint64 KZipFileEntry::compressedSize() const
1386 {
1387 return d->compressedSize;
1388 }
1389
setCompressedSize(qint64 compressedSize)1390 void KZipFileEntry::setCompressedSize(qint64 compressedSize)
1391 {
1392 d->compressedSize = compressedSize;
1393 }
1394
setHeaderStart(qint64 headerstart)1395 void KZipFileEntry::setHeaderStart(qint64 headerstart)
1396 {
1397 d->headerStart = headerstart;
1398 }
1399
headerStart() const1400 qint64 KZipFileEntry::headerStart() const
1401 {
1402 return d->headerStart;
1403 }
1404
crc32() const1405 unsigned long KZipFileEntry::crc32() const
1406 {
1407 return d->crc;
1408 }
1409
setCRC32(unsigned long crc32)1410 void KZipFileEntry::setCRC32(unsigned long crc32)
1411 {
1412 d->crc = crc32;
1413 }
1414
path() const1415 const QString &KZipFileEntry::path() const
1416 {
1417 return d->path;
1418 }
1419
data() const1420 QByteArray KZipFileEntry::data() const
1421 {
1422 QIODevice *dev = createDevice();
1423 QByteArray arr;
1424 if (dev) {
1425 arr = dev->readAll();
1426 delete dev;
1427 }
1428 return arr;
1429 }
1430
createDevice() const1431 QIODevice *KZipFileEntry::createDevice() const
1432 {
1433 // qCDebug(KArchiveLog) << "creating iodevice limited to pos=" << position() << ", csize=" << compressedSize();
1434 // Limit the reading to the appropriate part of the underlying device (e.g. file)
1435 KLimitedIODevice *limitedDev = new KLimitedIODevice(archive()->device(), position(), compressedSize());
1436 if (encoding() == 0 || compressedSize() == 0) { // no compression (or even no data)
1437 return limitedDev;
1438 }
1439
1440 if (encoding() == 8) {
1441 // On top of that, create a device that uncompresses the zlib data
1442 KCompressionDevice *filterDev = new KCompressionDevice(limitedDev, true, KCompressionDevice::GZip);
1443
1444 if (!filterDev) {
1445 return nullptr; // ouch
1446 }
1447 filterDev->setSkipHeaders(); // Just zlib, not gzip
1448 bool b = filterDev->open(QIODevice::ReadOnly);
1449 Q_UNUSED(b);
1450 Q_ASSERT(b);
1451 return filterDev;
1452 }
1453
1454 qCCritical(KArchiveLog) << "This zip file contains files compressed with method" << encoding() << ", this method is currently not supported by KZip,"
1455 << "please use a command-line tool to handle this file.";
1456 delete limitedDev;
1457 return nullptr;
1458 }
1459