/src/karchive/src/kzip.cpp
Line | Count | Source |
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 <QtEndian> |
22 | | #include <qplatformdefs.h> |
23 | | |
24 | | #include <string.h> |
25 | | #include <time.h> |
26 | | #include <zlib.h> |
27 | | |
28 | | #ifndef QT_STAT_LNK |
29 | | #define QT_STAT_LNK 0120000 |
30 | | #endif // QT_STAT_LNK |
31 | | |
32 | | static const int max_path_len = 4095; // maximum number of character a path may contain |
33 | | |
34 | | static quint16 parseUi16(const char *buffer) |
35 | 19.7M | { |
36 | 19.7M | return quint16((uchar)buffer[0] | (uchar)buffer[1] << 8); |
37 | 19.7M | } |
38 | | |
39 | | static uint parseUi32(const char *buffer) |
40 | 7.37M | { |
41 | 7.37M | return uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24); |
42 | 7.37M | } |
43 | | |
44 | | static quint64 parseUi64(const char *buffer) |
45 | 12.6k | { |
46 | 12.6k | const uint a = uint((uchar)buffer[0] | (uchar)buffer[1] << 8 | (uchar)buffer[2] << 16 | (uchar)buffer[3] << 24); |
47 | 12.6k | const uint b = uint((uchar)buffer[4] | (uchar)buffer[5] << 8 | (uchar)buffer[6] << 16 | (uchar)buffer[7] << 24); |
48 | 12.6k | return (a | (quint64)b << 32); |
49 | 12.6k | } |
50 | | |
51 | | static void transformToMsDos(const QDateTime &_dt, char *buffer) |
52 | 0 | { |
53 | 0 | const QDateTime dt = _dt.isValid() ? _dt : QDateTime::currentDateTime(); |
54 | | /* clang-format off */ |
55 | 0 | const quint16 time = (dt.time().hour() << 11) // 5 bit hour |
56 | 0 | | (dt.time().minute() << 5) // 6 bit minute |
57 | 0 | | (dt.time().second() >> 1); // 5 bit double seconds |
58 | | /* clang-format on */ |
59 | |
|
60 | 0 | buffer[0] = char(time); |
61 | 0 | buffer[1] = char(time >> 8); |
62 | | |
63 | | /* clang-format off */ |
64 | 0 | const quint16 date = ((dt.date().year() - 1980) << 9) // 7 bit year 1980-based |
65 | 0 | | (dt.date().month() << 5) // 4 bit month |
66 | 0 | | (dt.date().day()); // 5 bit day |
67 | | /* clang-format on */ |
68 | |
|
69 | 0 | buffer[2] = char(date); |
70 | 0 | buffer[3] = char(date >> 8); |
71 | 0 | } |
72 | | |
73 | | static uint transformFromMsDos(const char *buffer) |
74 | 2.82M | { |
75 | 2.82M | const quint16 time = parseUi16(buffer); |
76 | 2.82M | int h = time >> 11; |
77 | 2.82M | int m = (time & 0x7ff) >> 5; |
78 | 2.82M | int s = (time & 0x1f) * 2; |
79 | 2.82M | QTime qt(h, m, s); |
80 | | |
81 | 2.82M | const quint16 date = parseUi16(buffer + 2); |
82 | 2.82M | int y = (date >> 9) + 1980; |
83 | 2.82M | int o = (date & 0x1ff) >> 5; |
84 | 2.82M | int d = (date & 0x1f); |
85 | 2.82M | QDate qd(y, o, d); |
86 | | |
87 | 2.82M | QDateTime dt(qd, qt); |
88 | 2.82M | return dt.toSecsSinceEpoch(); |
89 | 2.82M | } |
90 | | |
91 | | // == parsing routines for zip headers |
92 | | |
93 | | /** all relevant information about parsing file information */ |
94 | | struct ParseFileInfo { |
95 | | // file related info |
96 | | mode_t perm; // permissions of this file |
97 | | // TODO: use quint32 instead of a uint? |
98 | | uint atime; // last access time (UNIX format) |
99 | | uint mtime; // modification time (UNIX format) |
100 | | uint ctime; // creation time (UNIX format) |
101 | | int uid; // user id (-1 if not specified) |
102 | | int gid; // group id (-1 if not specified) |
103 | | QByteArray guessed_symlink; // guessed symlink target |
104 | | uint dataoffset = 0; // offset from start of local header to data |
105 | | |
106 | | // parsing related info |
107 | | bool exttimestamp_seen; // true if extended timestamp extra field |
108 | | // has been parsed |
109 | | bool newinfounix_seen; // true if Info-ZIP Unix New extra field has |
110 | | // been parsed |
111 | | |
112 | | // file sizes from a ZIP64 extra field |
113 | | quint64 uncompressedSize = 0; |
114 | | quint64 compressedSize = 0; |
115 | | // position of the Local File Header itself, or from the |
116 | | // ZIP64 extra field in the Central Directory |
117 | | quint64 localheaderoffset = 0; |
118 | | |
119 | | ParseFileInfo() |
120 | 3.55M | : perm(0100644) |
121 | 3.55M | , uid(-1) |
122 | 3.55M | , gid(-1) |
123 | 3.55M | , exttimestamp_seen(false) |
124 | 3.55M | , newinfounix_seen(false) |
125 | 3.55M | { |
126 | 3.55M | ctime = mtime = atime = time(nullptr); |
127 | 3.55M | } |
128 | | }; |
129 | | |
130 | | /** updates the parse information with the given extended timestamp extra field. |
131 | | * @param buffer start content of buffer known to contain an extended |
132 | | * timestamp extra field (without magic & size) |
133 | | * @param size size of field content (must not count magic and size entries) |
134 | | * @param islocal true if this is a local field, false if central |
135 | | * @param pfi ParseFileInfo object to be updated |
136 | | * @return true if processing was successful |
137 | | */ |
138 | | static bool parseExtTimestamp(const char *buffer, int size, bool islocal, ParseFileInfo &pfi) |
139 | 126k | { |
140 | 126k | if (size < 1) { |
141 | | // qCDebug(KArchiveLog) << "premature end of extended timestamp (#1)"; |
142 | 98 | return false; |
143 | 98 | } /*end if*/ |
144 | 126k | int flags = *buffer; // read flags |
145 | 126k | buffer += 1; |
146 | 126k | size -= 1; |
147 | | |
148 | 126k | if (flags & 1) { // contains modification time |
149 | 120k | if (size < 4) { |
150 | | // qCDebug(KArchiveLog) << "premature end of extended timestamp (#2)"; |
151 | 1.14k | return false; |
152 | 1.14k | } /*end if*/ |
153 | 119k | pfi.mtime = parseUi32(buffer); |
154 | 119k | buffer += 4; |
155 | 119k | size -= 4; |
156 | 119k | } /*end if*/ |
157 | | // central extended field cannot contain more than the modification time |
158 | | // even if other flags are set |
159 | 125k | if (!islocal) { |
160 | 0 | pfi.exttimestamp_seen = true; |
161 | 0 | return true; |
162 | 0 | } /*end if*/ |
163 | | |
164 | 125k | if (flags & 2) { // contains last access time |
165 | 120k | if (size < 4) { |
166 | | // qCDebug(KArchiveLog) << "premature end of extended timestamp (#3)"; |
167 | 48.9k | return true; |
168 | 48.9k | } /*end if*/ |
169 | 71.1k | pfi.atime = parseUi32(buffer); |
170 | 71.1k | buffer += 4; |
171 | 71.1k | size -= 4; |
172 | 71.1k | } /*end if*/ |
173 | | |
174 | 76.0k | if (flags & 4) { // contains creation time |
175 | 8.40k | if (size < 4) { |
176 | | // qCDebug(KArchiveLog) << "premature end of extended timestamp (#4)"; |
177 | 4.01k | return true; |
178 | 4.01k | } /*end if*/ |
179 | 4.39k | pfi.ctime = parseUi32(buffer); |
180 | 4.39k | buffer += 4; |
181 | 4.39k | } /*end if*/ |
182 | | |
183 | 72.0k | pfi.exttimestamp_seen = true; |
184 | 72.0k | return true; |
185 | 76.0k | } |
186 | | |
187 | | /** updates the parse information with the given Info-ZIP Unix old extra field. |
188 | | * @param buffer start of content of buffer known to contain an Info-ZIP |
189 | | * Unix old extra field (without magic & size) |
190 | | * @param size size of field content (must not count magic and size entries) |
191 | | * @param islocal true if this is a local field, false if central |
192 | | * @param pfi ParseFileInfo object to be updated |
193 | | * @return true if processing was successful |
194 | | */ |
195 | | static bool parseInfoZipUnixOld(const char *buffer, int size, bool islocal, ParseFileInfo &pfi) |
196 | 31.1k | { |
197 | | // spec mandates to omit this field if one of the newer fields are available |
198 | 31.1k | if (pfi.exttimestamp_seen || pfi.newinfounix_seen) { |
199 | 883 | return true; |
200 | 883 | } |
201 | | |
202 | 30.2k | if (size < 8) { |
203 | | // qCDebug(KArchiveLog) << "premature end of Info-ZIP unix extra field old"; |
204 | 396 | return false; |
205 | 396 | } |
206 | | |
207 | 29.8k | pfi.atime = parseUi32(buffer); |
208 | 29.8k | pfi.mtime = parseUi32(buffer + 4); |
209 | 29.8k | if (islocal && size >= 12) { |
210 | 946 | pfi.uid = parseUi16(buffer + 8); |
211 | 946 | pfi.gid = parseUi16(buffer + 10); |
212 | 946 | } /*end if*/ |
213 | 29.8k | return true; |
214 | 30.2k | } |
215 | | |
216 | | #if 0 // not needed yet |
217 | | /** updates the parse information with the given Info-ZIP Unix new extra field. |
218 | | * @param buffer start of content of buffer known to contain an Info-ZIP |
219 | | * Unix new extra field (without magic & size) |
220 | | * @param size size of field content (must not count magic and size entries) |
221 | | * @param islocal true if this is a local field, false if central |
222 | | * @param pfi ParseFileInfo object to be updated |
223 | | * @return true if processing was successful |
224 | | */ |
225 | | static bool parseInfoZipUnixNew(const char *buffer, int size, bool islocal, |
226 | | ParseFileInfo &pfi) |
227 | | { |
228 | | if (!islocal) { // contains nothing in central field |
229 | | pfi.newinfounix = true; |
230 | | return true; |
231 | | } |
232 | | |
233 | | if (size < 4) { |
234 | | qCDebug(KArchiveLog) << "premature end of Info-ZIP unix extra field new"; |
235 | | return false; |
236 | | } |
237 | | |
238 | | pfi.uid = (uchar)buffer[0] | (uchar)buffer[1] << 8; |
239 | | buffer += 2; |
240 | | pfi.gid = (uchar)buffer[0] | (uchar)buffer[1] << 8; |
241 | | buffer += 2; |
242 | | |
243 | | pfi.newinfounix = true; |
244 | | return true; |
245 | | } |
246 | | #endif |
247 | | |
248 | | /** |
249 | | * parses the extra field |
250 | | * |
251 | | * the 64-bit values for uncompressed size, compressed size and local header offset |
252 | | * are retrieved only if the 32-bit values are 0xFFFFFFFF |
253 | | * |
254 | | * @param buffer start of buffer where the extra field is to be found |
255 | | * @param size size of the extra field |
256 | | * @param pfi ParseFileInfo object which to write the results into |
257 | | * @param uncompressedSize32 quint32 32-bit value for uncompressed size |
258 | | * @param compressedSize32 quint32 32-bit value for compressed size |
259 | | * @param localHeaderOffset32 quint32 32-bit value for local header offset |
260 | | * @return true if parsing was successful |
261 | | */ |
262 | | static bool parseExtraField(const char *buffer, int size, ParseFileInfo &pfi, quint32 uncompressedSize32, quint32 compressedSize32, quint32 localHeaderOffset32) |
263 | 2.89M | { |
264 | 3.34M | while (size >= 4) { // as long as a potential extra field can be read |
265 | 574k | int magic = parseUi16(buffer); |
266 | 574k | int fieldsize = parseUi16(buffer + 2); |
267 | 574k | buffer += 4; |
268 | 574k | size -= 4; |
269 | | |
270 | 574k | if (fieldsize > size) { |
271 | | // qCDebug(KArchiveLog) << "fieldsize: " << fieldsize << " size: " << size; |
272 | | // qCDebug(KArchiveLog) << "premature end of extra fields reached"; |
273 | 120k | break; |
274 | 120k | } |
275 | | |
276 | 453k | switch (magic) { |
277 | 16.4k | case 0x0001: { |
278 | | // ZIP64 extended file information |
279 | 16.4k | int offset = 0; |
280 | 16.4k | if (uncompressedSize32 == 0xFFFFFFFF && offset + 8 <= fieldsize) { |
281 | 2.05k | pfi.uncompressedSize = parseUi64(buffer + offset); |
282 | 2.05k | offset += 8; |
283 | 2.05k | } |
284 | 16.4k | if (compressedSize32 == 0xFFFFFFFF && offset + 8 <= fieldsize) { |
285 | 5.42k | pfi.compressedSize = parseUi64(buffer + offset); |
286 | 5.42k | offset += 8; |
287 | 5.42k | } |
288 | 16.4k | if (localHeaderOffset32 == 0xFFFFFFFF && offset + 8 <= fieldsize) { |
289 | 818 | pfi.localheaderoffset = parseUi64(buffer + offset); |
290 | 818 | offset += 8; |
291 | 818 | } |
292 | 16.4k | break; |
293 | 0 | } |
294 | 126k | case 0x5455: // extended timestamp |
295 | 126k | if (!parseExtTimestamp(buffer, fieldsize, true, pfi)) { |
296 | 1.24k | return false; |
297 | 1.24k | } |
298 | 125k | break; |
299 | 125k | case 0x5855: // old Info-ZIP unix extra field |
300 | 31.1k | if (!parseInfoZipUnixOld(buffer, fieldsize, true, pfi)) { |
301 | 396 | return false; |
302 | 396 | } |
303 | 30.7k | break; |
304 | | #if 0 // not needed yet |
305 | | case 0x7855: // new Info-ZIP unix extra field |
306 | | if (!parseInfoZipUnixNew(buffer, fieldsize, islocal, pfi)) { |
307 | | return false; |
308 | | } |
309 | | break; |
310 | | #endif |
311 | 279k | default: |
312 | | /* ignore everything else */ |
313 | 279k | ; |
314 | 453k | } /*end switch*/ |
315 | | |
316 | 452k | buffer += fieldsize; |
317 | 452k | size -= fieldsize; |
318 | 452k | } /*wend*/ |
319 | 2.89M | return true; |
320 | 2.89M | } |
321 | | |
322 | | /** |
323 | | * Advance the current file position to the next possible header location |
324 | | * |
325 | | * @param dev device that is read from |
326 | | * @param header storage location for the complete header |
327 | | * @param minSize required size of the header |
328 | | * @return true if a possible header location matching the 'PK' signature |
329 | | * bytes have been found, false if the end of file has been reached |
330 | | */ |
331 | | static bool seekAnyHeader(QIODevice *dev, QByteArray &header, qsizetype minSize) |
332 | 803k | { |
333 | 803k | header.resize(1024); |
334 | 803k | int n = dev->peek(header.data(), header.size()); |
335 | | |
336 | 1.41M | while (n >= minSize) { |
337 | 1.40M | if (auto i = QByteArrayView(header.data(), n).indexOf("PK"); i >= 0) { |
338 | 796k | dev->skip(i); |
339 | 796k | if ((i + minSize) < n) { |
340 | 794k | header.remove(0, i); |
341 | 794k | header.resize(minSize); |
342 | 794k | return true; |
343 | 794k | } else { |
344 | 2.74k | n = dev->peek(header.data(), minSize); |
345 | 2.74k | header.resize(n); |
346 | 2.74k | return n >= minSize; |
347 | 2.74k | } |
348 | 796k | } |
349 | 610k | dev->skip(n - 1); |
350 | 610k | n = dev->peek(header.data(), header.size()); |
351 | 610k | } |
352 | 6.46k | header.resize(n); |
353 | 6.46k | return false; |
354 | 803k | } |
355 | | |
356 | | /** |
357 | | * Advance the current file position to the next header following |
358 | | * a data descriptor |
359 | | * |
360 | | * @param dev device that is read from |
361 | | * @param isZip64 use Zip64 data descriptor layout |
362 | | * @return true if a data descriptor signature has been found, and the |
363 | | * compressed size matches the current position advance |
364 | | */ |
365 | | static bool seekPostDataDescriptor(QIODevice *dev, bool isZip64) |
366 | 9.11k | { |
367 | | // Both size fields are uint64 for Zip64, uint32 otherwise |
368 | 9.11k | const qsizetype descriptorSize = isZip64 ? 24 : 16; |
369 | | |
370 | 9.11k | QByteArray header; |
371 | 9.11k | const qint64 oldPos = dev->pos(); |
372 | | |
373 | 229k | while (seekAnyHeader(dev, header, descriptorSize)) { |
374 | | // qCDebug(KArchiveLog) << "Possible data descriptor header at" << dev->pos() << header; |
375 | 228k | if (header.startsWith("PK\x07\x08")) { |
376 | 14.2k | const qint64 compressedSize = isZip64 ? parseUi64(header.constData() + 8) // |
377 | 14.2k | : parseUi32(header.constData() + 8); |
378 | | // Do not move compressedSize to the left side of the check |
379 | | // we have on the left on purpose to prevent addition overflows |
380 | 14.2k | if ((compressedSize > 0) && (oldPos == dev->pos() - compressedSize)) { |
381 | 7.38k | dev->skip(descriptorSize); |
382 | 7.38k | return true; |
383 | 7.38k | } |
384 | 6.81k | dev->skip(4); // Skip found 'PK\7\8' |
385 | 213k | } else { |
386 | 213k | dev->skip(2); // Skip found 'PK' |
387 | 213k | } |
388 | 228k | } |
389 | 1.72k | return false; |
390 | 9.11k | } |
391 | | |
392 | | /** |
393 | | * Advance the current file position to the next header |
394 | | * @param dev device that is read from |
395 | | * @return true if a local or central header token could be reached, false on error |
396 | | */ |
397 | | static bool seekToNextHeaderToken(QIODevice *dev) |
398 | 297k | { |
399 | 297k | QByteArray header; |
400 | | |
401 | 448k | while (seekAnyHeader(dev, header, 4)) { |
402 | | // qCDebug(KArchiveLog) << "Possible header at" << dev->pos() << header; |
403 | | // PK34 for the next local header in case there is no data descriptor |
404 | | // PK12 for the central header in case there is no data descriptor |
405 | 445k | if (header.startsWith("PK\x03\x04") || header.startsWith("PK\x01\x02")) { |
406 | 294k | return true; |
407 | 294k | } else { |
408 | 151k | dev->skip(2); // Skip found 'PK' |
409 | 151k | } |
410 | 445k | } |
411 | 3.06k | return false; |
412 | 297k | } |
413 | | |
414 | | //////////////////////////////////////////////////////////////////////// |
415 | | /////////////////////////// KZip /////////////////////////////////////// |
416 | | //////////////////////////////////////////////////////////////////////// |
417 | | |
418 | | class Q_DECL_HIDDEN KZip::KZipPrivate |
419 | | { |
420 | | public: |
421 | | KZipPrivate() |
422 | 72.5k | : m_crc(0) |
423 | 72.5k | , m_currentFile(nullptr) |
424 | 72.5k | , m_currentDev(nullptr) |
425 | 72.5k | , m_compression(8) |
426 | 72.5k | , m_extraField(KZip::NoExtraField) |
427 | 72.5k | , m_offset(0) |
428 | 72.5k | { |
429 | 72.5k | } |
430 | | |
431 | | unsigned long m_crc; // checksum |
432 | | KZipFileEntry *m_currentFile; // file currently being written |
433 | | QIODevice *m_currentDev; // filterdev used to write to the above file |
434 | | QList<KZipFileEntry *> m_fileList; // flat list of all files, for the index (saves a recursive method ;) |
435 | | int m_compression; |
436 | | KZip::ExtraField m_extraField; |
437 | | // m_offset holds the offset of the place in the zip, |
438 | | // where new data can be appended. after openarchive it points to 0, when in |
439 | | // writeonly mode, or it points to the beginning of the central directory. |
440 | | // each call to writefile updates this value. |
441 | | quint64 m_offset; |
442 | | // Position of the first Local File Header |
443 | | quint64 m_startPos = 0; |
444 | | }; |
445 | | |
446 | | KZip::KZip(const QString &fileName) |
447 | 72.5k | : KArchive(fileName) |
448 | 72.5k | , d(new KZipPrivate) |
449 | 72.5k | { |
450 | 72.5k | } |
451 | | |
452 | | KZip::KZip(QIODevice *dev) |
453 | 0 | : KArchive(dev) |
454 | 0 | , d(new KZipPrivate) |
455 | 0 | { |
456 | 0 | } |
457 | | |
458 | | KZip::~KZip() |
459 | 72.5k | { |
460 | | // qCDebug(KArchiveLog) << this; |
461 | 72.5k | if (isOpen()) { |
462 | 72.5k | close(); |
463 | 72.5k | } |
464 | 72.5k | delete d; |
465 | 72.5k | } |
466 | | |
467 | | bool KZip::openArchive(QIODevice::OpenMode mode) |
468 | 72.5k | { |
469 | | // qCDebug(KArchiveLog); |
470 | 72.5k | d->m_fileList.clear(); |
471 | | |
472 | 72.5k | if (mode == QIODevice::WriteOnly) { |
473 | 0 | return true; |
474 | 0 | } |
475 | | |
476 | 72.5k | char buffer[47]; |
477 | | |
478 | | // Check that it's a valid ZIP file |
479 | | // KArchive::open() opened the underlying device already. |
480 | | |
481 | | // contains information gathered from the local file headers |
482 | 72.5k | QHash<QByteArray, ParseFileInfo> pfi_map; |
483 | | |
484 | 72.5k | QIODevice *dev = device(); |
485 | | |
486 | | // We set a bool for knowing if we are allowed to skip the start of the file |
487 | 72.5k | bool startOfFile = true; |
488 | | |
489 | 72.5k | qint64 expectedStartPos = 0; |
490 | | |
491 | 3.30M | for (;;) { // repeat until 'end of entries' signature is reached |
492 | | // qCDebug(KArchiveLog) << "loop starts"; |
493 | | // qCDebug(KArchiveLog) << "dev->pos() now : " << dev->pos(); |
494 | 3.30M | int n = dev->read(buffer, 4); |
495 | | |
496 | 3.30M | if (n < 4) { |
497 | 6.00k | setErrorString(tr("Invalid ZIP file. Unexpected end of file. (Error code: %1)").arg(1)); |
498 | 6.00k | return false; |
499 | 6.00k | } |
500 | | |
501 | 3.29M | if (!memcmp(buffer, "PK\6\6", 4)) { // 'Zip64 end of central directory record' |
502 | 13.1k | startOfFile = false; |
503 | 13.1k | break; |
504 | 13.1k | } |
505 | | |
506 | 3.28M | if (!memcmp(buffer, "PK\5\6", 4)) { // 'end of entries' |
507 | | // qCDebug(KArchiveLog) << "PK56 found end of archive at offset" << dev->pos(); |
508 | 29.8k | startOfFile = false; |
509 | 29.8k | break; |
510 | 29.8k | } |
511 | | |
512 | 3.25M | if (!memcmp(buffer, "PK\3\4", 4)) { // local file header |
513 | | // qCDebug(KArchiveLog) << "PK34 found local file header at offset" << dev->pos(); |
514 | 2.82M | startOfFile = false; |
515 | | |
516 | 2.82M | ParseFileInfo pfi; |
517 | 2.82M | pfi.localheaderoffset = dev->pos() - 4; |
518 | | |
519 | | // read static header stuff |
520 | 2.82M | n = dev->read(buffer, 26); |
521 | 2.82M | if (n < 26) { |
522 | 1.19k | setErrorString(tr("Invalid ZIP file. Unexpected end of file. (Error code: %1)").arg(4)); |
523 | 1.19k | return false; |
524 | 1.19k | } |
525 | 2.82M | int neededVersion = parseUi16(buffer); |
526 | 2.82M | bool isZip64 = neededVersion >= 45; |
527 | | |
528 | 2.82M | int gpf = (uchar)buffer[2]; // "general purpose flag" not "general protection fault" ;-) |
529 | 2.82M | int compression_mode = parseUi16(buffer + 4); |
530 | 2.82M | uint mtime = transformFromMsDos(buffer + 6); |
531 | | |
532 | 2.82M | qint64 compr_size = parseUi32(buffer + 14); |
533 | 2.82M | qint64 uncomp_size = parseUi32(buffer + 18); |
534 | 2.82M | const int namelen = parseUi16(buffer + 22); |
535 | 2.82M | const int extralen = parseUi16(buffer + 24); |
536 | | |
537 | | /* |
538 | | qCDebug(KArchiveLog) << "general purpose bit flag: " << gpf; |
539 | | qCDebug(KArchiveLog) << "compressed size: " << compr_size; |
540 | | qCDebug(KArchiveLog) << "uncompressed size: " << uncomp_size; |
541 | | qCDebug(KArchiveLog) << "namelen: " << namelen; |
542 | | qCDebug(KArchiveLog) << "extralen: " << extralen; |
543 | | qCDebug(KArchiveLog) << "archive size: " << dev->size(); |
544 | | */ |
545 | | |
546 | | // read fileName |
547 | 2.82M | if (namelen <= 0) { |
548 | 374 | setErrorString(tr("Invalid ZIP file. Negative name length")); |
549 | 374 | return false; |
550 | 374 | } |
551 | 2.82M | QByteArray fileName = dev->read(namelen); |
552 | 2.82M | if (fileName.size() < namelen) { |
553 | 1.95k | setErrorString(tr("Invalid ZIP file. Name not completely read (#2)")); |
554 | 1.95k | return false; |
555 | 1.95k | } |
556 | | |
557 | 2.81M | pfi.mtime = mtime; |
558 | 2.81M | pfi.dataoffset = 30 + extralen + namelen; |
559 | | |
560 | | // read and parse the beginning of the extra field, |
561 | | // skip rest of extra field in case it is too long |
562 | 2.81M | quint64 extraFieldEnd = dev->pos() + extralen; |
563 | 2.81M | int handledextralen = qMin(extralen, (int)sizeof buffer); |
564 | | |
565 | | // if (handledextralen) |
566 | | // qCDebug(KArchiveLog) << "handledextralen: " << handledextralen; |
567 | | |
568 | 2.81M | n = dev->read(buffer, handledextralen); |
569 | | // no error msg necessary as we deliberately truncate the extra field |
570 | 2.81M | if (!parseExtraField(buffer, n, pfi, uncomp_size, compr_size, 0)) { |
571 | 100 | setErrorString(tr("Invalid ZIP File. Broken ExtraField.")); |
572 | 100 | return false; |
573 | 100 | } |
574 | | |
575 | 2.81M | if (compr_size == 0xFFFFFFFF) { |
576 | 6.87k | compr_size = pfi.compressedSize; |
577 | 6.87k | } |
578 | 2.81M | if (uncomp_size == 0xFFFFFFFF) { |
579 | 8.74k | uncomp_size = pfi.uncompressedSize; |
580 | 8.74k | } |
581 | | |
582 | | // jump to end of extra field |
583 | 2.81M | dev->seek(extraFieldEnd); |
584 | | |
585 | | // we have to take care of the 'general purpose bit flag'. |
586 | | // if bit 3 is set, the header doesn't contain the length of |
587 | | // the file and we look for the signature 'PK\7\8'. |
588 | 2.81M | if (gpf & 8) { |
589 | | // here we have to read through the compressed data to find |
590 | | // the next PKxx |
591 | 9.11k | if (!seekPostDataDescriptor(dev, isZip64)) { |
592 | 1.72k | setErrorString(tr("Could not seek to next header token")); |
593 | 1.72k | return false; |
594 | 1.72k | } |
595 | 2.81M | } else { |
596 | | // here we skip the compressed data and jump to the next header |
597 | | // qCDebug(KArchiveLog) << "general purpose bit flag indicates, that local file header contains valid size"; |
598 | 2.81M | bool foundSignature = false; |
599 | | // check if this could be a symbolic link |
600 | 2.81M | if (compression_mode == NoCompression // |
601 | 2.46M | && uncomp_size <= max_path_len // |
602 | 2.44M | && uncomp_size > 0) { |
603 | | // read content and store it |
604 | | // If it's not a symlink, then we'll just discard the data for now. |
605 | 2.42M | pfi.guessed_symlink = dev->read(uncomp_size); |
606 | 2.42M | if (pfi.guessed_symlink.size() < uncomp_size) { |
607 | 332 | setErrorString(tr("Invalid ZIP file. Unexpected end of file. (#5)")); |
608 | 332 | return false; |
609 | 332 | } |
610 | 2.42M | } else { |
611 | 390k | if (compr_size > dev->size()) { |
612 | | // here we cannot trust the compressed size, so scan through the compressed |
613 | | // data to find the next header |
614 | 297k | if (!seekToNextHeaderToken(dev)) { |
615 | 3.06k | setErrorString(tr("Could not seek to next header token")); |
616 | 3.06k | return false; |
617 | 3.06k | } |
618 | 294k | foundSignature = true; |
619 | 294k | } else { |
620 | | // qCDebug(KArchiveLog) << "before interesting dev->pos(): " << dev->pos(); |
621 | 93.0k | if (dev->skip(compr_size) != compr_size) { |
622 | 1.08k | setErrorString(tr("Could not seek to file compressed size")); |
623 | 1.08k | return false; |
624 | 1.08k | } |
625 | | /* qCDebug(KArchiveLog) << "after interesting dev->pos(): " << dev->pos(); |
626 | | if (success) |
627 | | qCDebug(KArchiveLog) << "dev->at was successful... "; |
628 | | else |
629 | | qCDebug(KArchiveLog) << "dev->at failed... ";*/ |
630 | 93.0k | } |
631 | 390k | } |
632 | | // test for optional data descriptor |
633 | 2.80M | if (!foundSignature) { |
634 | | // qCDebug(KArchiveLog) << "Testing for optional data descriptor"; |
635 | | // read static data descriptor |
636 | 2.51M | n = dev->peek(buffer, 4); |
637 | 2.51M | if (n < 4) { |
638 | 726 | setErrorString(tr("Invalid ZIP file. Unexpected end of file. (#1)")); |
639 | 726 | return false; |
640 | 726 | } |
641 | | |
642 | 2.51M | QByteArrayView currentHead(buffer, 4); |
643 | | // qCDebug(KArchiveLog) << "Testing for optional data descriptor @ " << dev->pos() << currentHead; |
644 | 2.51M | if (currentHead.startsWith("PK\x07\x08")) { |
645 | 616 | dev->skip(16); // skip rest of the 'data_descriptor' |
646 | 2.51M | } else if (!(currentHead.startsWith("PK\x03\x04") || currentHead.startsWith("PK\x01\x02"))) { |
647 | | // assume data descriptor without signature |
648 | 7.93k | dev->skip(12); // skip rest of the 'data_descriptor' |
649 | 7.93k | } |
650 | 2.51M | } |
651 | | |
652 | | // not needed any more |
653 | | /* // here we calculate the length of the file in the zip |
654 | | // with headers and jump to the next header. |
655 | | uint skip = compr_size + namelen + extralen; |
656 | | offset += 30 + skip;*/ |
657 | 2.80M | } |
658 | 2.81M | pfi_map.insert(fileName, pfi); |
659 | 2.81M | } else if (!memcmp(buffer, "PK\1\2", 4)) { // central block |
660 | | // qCDebug(KArchiveLog) << "PK12 found central block at offset" << dev->pos(); |
661 | 368k | startOfFile = false; |
662 | | |
663 | | // so we reached the central header at the end of the zip file |
664 | | // here we get all interesting data out of the central header |
665 | | // of a file |
666 | 368k | auto offset = dev->pos() - 4; |
667 | | |
668 | | // set offset for appending new files |
669 | 368k | if (d->m_offset == 0) { |
670 | 61.9k | d->m_offset = offset; |
671 | 61.9k | } |
672 | | |
673 | 368k | n = dev->read(buffer + 4, 42); |
674 | 368k | if (n < 42) { |
675 | 1.10k | setErrorString(tr("Invalid ZIP file, central entry too short (not long enough for valid entry)")); |
676 | 1.10k | return false; |
677 | 1.10k | } |
678 | | |
679 | | // length of extra attributes |
680 | 367k | const int extralen = parseUi16(buffer + 30); |
681 | | // length of comment for this file |
682 | 367k | const int commlen = parseUi16(buffer + 32); |
683 | | // compression method of this file |
684 | 367k | const int cmethod = parseUi16(buffer + 10); |
685 | | // int gpf = parseUi16(buffer + 8); |
686 | | // qCDebug(KArchiveLog) << "general purpose flag=" << gpf; |
687 | | // length of the fileName (well, pathname indeed) |
688 | 367k | const int namelen = parseUi16(buffer + 28); |
689 | 367k | if (namelen <= 0) { |
690 | 170 | setErrorString(tr("Invalid ZIP file, file path name length is zero")); |
691 | 170 | return false; |
692 | 170 | } |
693 | 366k | const int varDataDesiredLength = namelen + extralen; |
694 | 366k | const QByteArray varData = dev->read(varDataDesiredLength); |
695 | 366k | if (varData.length() < varDataDesiredLength) { |
696 | 1.33k | setErrorString(tr("Invalid ZIP file, unable to read %1 + %2 bytes for filename and extra data at offset %3") |
697 | 1.33k | .arg(namelen, extralen, dev->pos() - varData.size())); |
698 | 1.33k | return false; |
699 | 1.33k | } |
700 | | |
701 | | // crc32 of the file |
702 | 365k | uint crc32 = parseUi32(buffer + 16); |
703 | | // 32 bit uncompressed file size |
704 | 365k | quint32 ucsize32 = parseUi32(buffer + 24); |
705 | | // 32 bit compressed file size |
706 | 365k | quint32 csize32 = parseUi32(buffer + 20); |
707 | | // 32 bit offset of local header |
708 | 365k | quint32 localheaderoffset_32 = parseUi32(buffer + 42); |
709 | | |
710 | 365k | ParseFileInfo extrafi; |
711 | 365k | if (extralen) { |
712 | 74.5k | parseExtraField(varData.constData() + namelen, extralen, extrafi, ucsize32, csize32, localheaderoffset_32); |
713 | 74.5k | } |
714 | | |
715 | | // uncompressed file size |
716 | 365k | quint64 ucsize = (ucsize32 == 0xFFFFFFFF) ? extrafi.uncompressedSize : ucsize32; |
717 | | // compressed file size |
718 | 365k | quint64 csize = (csize32 == 0xFFFFFFFF) ? extrafi.compressedSize : csize32; |
719 | | // offset of local header |
720 | 365k | quint64 localheaderoffset = (localheaderoffset_32 == 0xFFFFFFFF) ? extrafi.localheaderoffset : localheaderoffset_32; |
721 | | |
722 | 365k | QByteArray bufferName(varData.constData(), namelen); |
723 | | |
724 | 365k | ParseFileInfo pfi = pfi_map.value(bufferName, ParseFileInfo()); |
725 | | |
726 | 365k | QString name(QFile::decodeName(bufferName)); |
727 | | |
728 | | // qCDebug(KArchiveLog) << "name: " << name; |
729 | | // qCDebug(KArchiveLog) << "cmethod: " << cmethod; |
730 | | // qCDebug(KArchiveLog) << "extralen: " << extralen; |
731 | | // qCDebug(KArchiveLog) << "localheader dataoffset: " << pfi.dataoffset; |
732 | | |
733 | | // offset, where the real data for uncompression starts |
734 | 365k | qint64 dataoffset = localheaderoffset + pfi.dataoffset; |
735 | 365k | if (pfi.localheaderoffset != expectedStartPos + localheaderoffset) { |
736 | 13.0k | if (pfi.localheaderoffset == d->m_startPos + localheaderoffset) { |
737 | 10.1k | qCDebug(KArchiveLog) << "warning:" << d->m_startPos << "extra bytes at beginning of zipfile"; |
738 | 10.1k | expectedStartPos = d->m_startPos; |
739 | 10.1k | } else { |
740 | 2.86k | setErrorString(tr("Invalid ZIP file, inconsistent Local Header Offset in Central Directory at %1").arg(offset)); |
741 | 2.86k | return false; |
742 | 2.86k | } |
743 | 10.1k | dataoffset = localheaderoffset + pfi.dataoffset + d->m_startPos; |
744 | 10.1k | } |
745 | | |
746 | | // qCDebug(KArchiveLog) << "csize: " << csize; |
747 | | |
748 | 362k | int os_madeby = (uchar)buffer[5]; |
749 | 362k | bool isdir = false; |
750 | 362k | int access = 0100644; |
751 | | |
752 | 362k | if (os_madeby == 3) { // good ole unix |
753 | 246k | access = parseUi16(buffer + 40); |
754 | 246k | } |
755 | | |
756 | 362k | QString entryName; |
757 | | |
758 | 362k | if (name.endsWith(QLatin1Char('/'))) { // Entries with a trailing slash are directories |
759 | 39.0k | isdir = true; |
760 | 39.0k | name = name.left(name.length() - 1); |
761 | 39.0k | if (os_madeby != 3) { |
762 | 20.7k | access = S_IFDIR | 0755; |
763 | 20.7k | } else { |
764 | 18.3k | access |= S_IFDIR | 0700; |
765 | 18.3k | } |
766 | 39.0k | } |
767 | | |
768 | 362k | int pos = name.lastIndexOf(QLatin1Char('/')); |
769 | 362k | if (pos == -1) { |
770 | 149k | entryName = name; |
771 | 213k | } else { |
772 | 213k | entryName = name.mid(pos + 1); |
773 | 213k | } |
774 | 362k | if (entryName.isEmpty()) { |
775 | 154 | setErrorString(tr("Invalid ZIP file, found empty entry name")); |
776 | 154 | return false; |
777 | 154 | } |
778 | | |
779 | 362k | KArchiveEntry *entry; |
780 | 362k | if (isdir) { |
781 | 38.9k | QString path = QDir::cleanPath(name); |
782 | 38.9k | const KArchiveEntry *ent = rootDir()->entry(path); |
783 | 38.9k | if (ent && ent->isDirectory()) { |
784 | | // qCDebug(KArchiveLog) << "Directory already exists, NOT going to add it again"; |
785 | 14.3k | entry = nullptr; |
786 | 24.6k | } else { |
787 | 24.6k | QDateTime mtime = KArchivePrivate::time_tToDateTime(pfi.mtime); |
788 | 24.6k | entry = new KArchiveDirectory(this, entryName, access, mtime, rootDir()->user(), rootDir()->group(), QString()); |
789 | | // qCDebug(KArchiveLog) << "KArchiveDirectory created, entryName= " << entryName << ", name=" << name; |
790 | 24.6k | } |
791 | 323k | } else { |
792 | 323k | QString symlink; |
793 | 323k | if ((access & QT_STAT_MASK) == QT_STAT_LNK) { |
794 | 14.0k | symlink = QFile::decodeName(pfi.guessed_symlink); |
795 | 14.0k | } |
796 | 323k | QDateTime mtime = KArchivePrivate::time_tToDateTime(pfi.mtime); |
797 | 323k | entry = |
798 | 323k | new KZipFileEntry(this, entryName, access, mtime, rootDir()->user(), rootDir()->group(), symlink, name, dataoffset, ucsize, cmethod, csize); |
799 | 323k | static_cast<KZipFileEntry *>(entry)->setHeaderStart(localheaderoffset); |
800 | 323k | static_cast<KZipFileEntry *>(entry)->setCRC32(crc32); |
801 | | // qCDebug(KArchiveLog) << "KZipFileEntry created, entryName= " << entryName << ", name=" << name; |
802 | 323k | d->m_fileList.append(static_cast<KZipFileEntry *>(entry)); |
803 | 323k | } |
804 | | |
805 | 362k | if (entry) { |
806 | 348k | if (pos == -1) { |
807 | | // We don't want to fail opening potentially malformed files, so void the return value |
808 | 145k | (void)rootDir()->addEntryV2(entry); |
809 | 202k | } else { |
810 | | // In some tar files we can find dir/./file => call cleanPath |
811 | 202k | QString path = QDir::cleanPath(name.left(pos)); |
812 | | // Ensure container directory exists, create otherwise |
813 | 202k | KArchiveDirectory *tdir = findOrCreate(path); |
814 | 202k | if (tdir) { |
815 | 201k | (void)tdir->addEntryV2(entry); |
816 | 201k | } else { |
817 | 1.02k | setErrorString(tr("File %1 is in folder %2, but %3 is actually a file.").arg(entryName, path, path)); |
818 | 1.02k | delete entry; |
819 | 1.02k | return false; |
820 | 1.02k | } |
821 | 202k | } |
822 | 348k | } |
823 | | |
824 | | // calculate offset to next entry |
825 | 361k | offset += 46 + commlen + extralen + namelen; |
826 | 361k | const bool b = dev->seek(offset); |
827 | 361k | if (!b) { |
828 | 0 | setErrorString(tr("Could not seek to next entry")); |
829 | 0 | return false; |
830 | 0 | } |
831 | 361k | } else if (startOfFile) { |
832 | | // The file does not start with any ZIP header (e.g. self-extractable ZIP files) |
833 | | // Therefore we need to find the first PK\003\004 (local header) |
834 | | // qCDebug(KArchiveLog) << "Try to skip start of file"; |
835 | 56.8k | startOfFile = false; |
836 | 56.8k | bool foundSignature = false; |
837 | | |
838 | 56.8k | QByteArray header; |
839 | 124k | while (seekAnyHeader(dev, header, 4)) { |
840 | | // We have to detect the magic token for a local header: PK\003\004 |
841 | | /* |
842 | | * Note: we do not need to check the other magics, if the ZIP file has no |
843 | | * local header, then it has not any files! |
844 | | */ |
845 | 122k | if (header.startsWith("PK\x03\x04")) { |
846 | 54.8k | foundSignature = true; |
847 | 54.8k | d->m_startPos = dev->pos(); |
848 | 54.8k | break; |
849 | 54.8k | } |
850 | 68.0k | if (dev->pos() > 4 * 1024 * 1024) { |
851 | 0 | break; |
852 | 0 | } |
853 | 68.0k | dev->skip(2); // Skip found 'PK' |
854 | 68.0k | } |
855 | | |
856 | 56.8k | if (!foundSignature) { |
857 | 1.94k | setErrorString(tr("Not a ZIP file, no Local File Header found.")); |
858 | 1.94k | return false; |
859 | 1.94k | } |
860 | 56.8k | } else { |
861 | 4.44k | setErrorString(tr("Invalid ZIP file. Unrecognized header at offset %1").arg(dev->pos() - 4)); |
862 | 4.44k | return false; |
863 | 4.44k | } |
864 | 3.25M | } |
865 | | // qCDebug(KArchiveLog) << "*** done *** "; |
866 | 42.9k | return true; |
867 | 72.5k | } |
868 | | |
869 | | bool KZip::closeArchive() |
870 | 72.5k | { |
871 | 72.5k | if (!(mode() & QIODevice::WriteOnly)) { |
872 | | // qCDebug(KArchiveLog) << "readonly"; |
873 | 72.5k | return true; |
874 | 72.5k | } |
875 | | |
876 | | // ReadWrite or WriteOnly |
877 | | // write all central dir file entries |
878 | | |
879 | | // to be written at the end of the file... |
880 | 0 | uLong crc = crc32(0L, nullptr, 0); |
881 | |
|
882 | 0 | qint64 centraldiroffset = device()->pos(); |
883 | | // qCDebug(KArchiveLog) << "closearchive: centraldiroffset: " << centraldiroffset; |
884 | 0 | qint64 atbackup = centraldiroffset; |
885 | 0 | for (KZipFileEntry *entry : d->m_fileList) { |
886 | | // set crc and compressed size in each local file header |
887 | 0 | if (!device()->seek(entry->headerStart() + 14)) { |
888 | 0 | setErrorString(tr("Could not seek to next file header: %1").arg(device()->errorString())); |
889 | 0 | return false; |
890 | 0 | } |
891 | | // qCDebug(KArchiveLog) << "closearchive setcrcandcsize: fileName:" |
892 | | // << entry->path() |
893 | | // << "encoding:" << entry->encoding(); |
894 | | |
895 | 0 | QByteArray path = QFile::encodeName(entry->path()); |
896 | 0 | uLong mycrc = entry->crc32(); |
897 | 0 | qint64 compressedSize = entry->compressedSize(); |
898 | 0 | qint64 size = entry->size(); |
899 | |
|
900 | 0 | bool compressedSizeNeeds64Bit = compressedSize >= 0xFFFFFFFF; |
901 | 0 | bool uncompressedSizeNeeds64Bit = size >= 0xFFFFFFFF; |
902 | 0 | bool isZip64File = (compressedSizeNeeds64Bit || uncompressedSizeNeeds64Bit); |
903 | |
|
904 | 0 | char buffer[12]; |
905 | | |
906 | | // crc checksum |
907 | 0 | qToLittleEndian<quint32>(mycrc, buffer); |
908 | |
|
909 | 0 | if (!isZip64File) { |
910 | 0 | qToLittleEndian<quint32>(compressedSize, buffer + 4); |
911 | 0 | qToLittleEndian<quint32>(size, buffer + 8); |
912 | |
|
913 | 0 | if (device()->write(buffer, 12) != 12) { |
914 | 0 | setErrorString(tr("Could not write file header: %1").arg(device()->errorString())); |
915 | 0 | return false; |
916 | 0 | } |
917 | 0 | } else { |
918 | | // compressed file size |
919 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, buffer + 4); |
920 | | // uncompressed file size |
921 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, buffer + 8); |
922 | |
|
923 | 0 | if (device()->write(buffer, 12) != 12) { |
924 | 0 | setErrorString(tr("Could not write file header: %1").arg(device()->errorString())); |
925 | 0 | return false; |
926 | 0 | } |
927 | | |
928 | | // jump to the Local File Header Zip64 extra field to fill the 64-bit sizes |
929 | 0 | qint64 extraFieldOffset = entry->headerStart() + 30 + path.length(); |
930 | 0 | if (d->m_extraField == ModificationTime) { |
931 | 0 | extraFieldOffset += 17; |
932 | 0 | } |
933 | | // skip Zip64 extra field header id and field size |
934 | 0 | extraFieldOffset += 4; |
935 | 0 | if (!device()->seek(extraFieldOffset)) { |
936 | 0 | setErrorString(tr("Could not seek to Zip64 extra field: %1").arg(device()->errorString())); |
937 | 0 | return false; |
938 | 0 | } |
939 | | |
940 | 0 | char z64Buffer[16]; |
941 | 0 | qToLittleEndian<quint64>(size, z64Buffer); |
942 | 0 | qToLittleEndian<quint64>(compressedSize, z64Buffer + 8); |
943 | 0 | if (device()->write(z64Buffer, 16) != 16) { |
944 | 0 | setErrorString(tr("Could not write Zip64 compressed size: %1").arg(device()->errorString())); |
945 | 0 | return false; |
946 | 0 | } |
947 | 0 | } |
948 | 0 | } |
949 | | |
950 | 0 | device()->seek(atbackup); |
951 | |
|
952 | 0 | for (KZipFileEntry *entry : d->m_fileList) { |
953 | | // qCDebug(KArchiveLog) << "fileName:" << entry->path() |
954 | | // << "encoding:" << entry->encoding(); |
955 | |
|
956 | 0 | QByteArray path = QFile::encodeName(entry->path()); |
957 | 0 | qint64 compressedSize = entry->compressedSize(); |
958 | 0 | qint64 size = entry->size(); |
959 | 0 | qint64 myhst = entry->headerStart(); |
960 | |
|
961 | 0 | bool compressedSizeNeeds64Bit = compressedSize >= 0xFFFFFFFF; |
962 | 0 | bool uncompressedSizeNeeds64Bit = size >= 0xFFFFFFFF; |
963 | 0 | bool offsetNeeds64Bit = myhst >= 0xFFFFFFFF; |
964 | 0 | bool entryNeedsZip64 = (compressedSizeNeeds64Bit || uncompressedSizeNeeds64Bit || offsetNeeds64Bit); |
965 | |
|
966 | 0 | int extra_field_len = (d->m_extraField == ModificationTime) ? 9 : 0; |
967 | 0 | uint zip64FieldSize = 0; |
968 | 0 | if (entryNeedsZip64) { |
969 | | // 2 header id + 2 field size + |
970 | | // 8 uncompressed size + 8 compressed size + 8 offset |
971 | 0 | zip64FieldSize = 28; |
972 | 0 | } |
973 | 0 | extra_field_len += zip64FieldSize; |
974 | |
|
975 | 0 | const int bufferSize = extra_field_len + path.length() + 46; |
976 | 0 | char *buffer = new char[bufferSize]; |
977 | |
|
978 | 0 | memset(buffer, 0, 46); // zero is a nice default for most header fields |
979 | | |
980 | | // Central header signature |
981 | 0 | buffer[0] = 'P'; |
982 | 0 | buffer[1] = 'K'; |
983 | 0 | buffer[2] = 1; |
984 | 0 | buffer[3] = 2; |
985 | | |
986 | | // Version made by (UNIX) |
987 | 0 | buffer[4] = 0x14; |
988 | 0 | buffer[5] = 3; |
989 | | // Version needed to extract |
990 | 0 | buffer[6] = entryNeedsZip64 ? 45 : 0x14; |
991 | 0 | buffer[7] = 0; |
992 | | |
993 | | // General purpose bit flag |
994 | | // qToLittleEndian<quint16>(0, buffer + 8); |
995 | | |
996 | | // compression method |
997 | 0 | qToLittleEndian<quint16>(char(entry->encoding()), buffer + 10); |
998 | |
|
999 | 0 | transformToMsDos(entry->date(), &buffer[12]); |
1000 | |
|
1001 | 0 | uLong mycrc = entry->crc32(); |
1002 | | // crc checksum |
1003 | 0 | qToLittleEndian<quint32>(mycrc, buffer + 16); |
1004 | | |
1005 | | // compressed file size |
1006 | 0 | if (entryNeedsZip64) { |
1007 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, buffer + 20); |
1008 | 0 | } else { |
1009 | 0 | qToLittleEndian<quint32>(compressedSize, buffer + 20); |
1010 | 0 | } |
1011 | | |
1012 | | // uncompressed file size |
1013 | 0 | if (entryNeedsZip64) { |
1014 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, buffer + 24); |
1015 | 0 | } else { |
1016 | 0 | qToLittleEndian<quint32>(size, buffer + 24); |
1017 | 0 | } |
1018 | | |
1019 | | // fileName length |
1020 | 0 | qToLittleEndian<quint16>(path.length(), buffer + 28); |
1021 | | |
1022 | | // extra field length |
1023 | 0 | qToLittleEndian<quint16>(extra_field_len, buffer + 30); |
1024 | | |
1025 | | // file comment length |
1026 | | // qToLittleEndian<quint16>(0, buffer + 32); |
1027 | | |
1028 | | // disk number where file starts |
1029 | | // qToLittleEndian<quint16>(0, buffer + 34); |
1030 | | |
1031 | | // internal file attributes |
1032 | | // none |
1033 | | // external file attributes |
1034 | 0 | qToLittleEndian<quint32>(entry->permissions(), buffer + 40); |
1035 | | |
1036 | | // relative offset of local header |
1037 | 0 | if (entryNeedsZip64) { |
1038 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, buffer + 42); |
1039 | 0 | } else { |
1040 | 0 | qToLittleEndian<quint32>(myhst, buffer + 42); |
1041 | 0 | } |
1042 | | |
1043 | | // file name |
1044 | 0 | strncpy(buffer + 46, path.constData(), path.length()); |
1045 | | // qCDebug(KArchiveLog) << "closearchive length to write: " << bufferSize; |
1046 | | |
1047 | | // extra field |
1048 | 0 | char *extfield = buffer + 46 + path.length(); |
1049 | 0 | if (d->m_extraField == ModificationTime) { |
1050 | | // "Extended timestamp" header (0x5455) |
1051 | 0 | extfield[0] = 'U'; |
1052 | 0 | extfield[1] = 'T'; |
1053 | 0 | extfield[2] = 5; // data size |
1054 | 0 | extfield[3] = 0; |
1055 | 0 | extfield[4] = 1 | 2 | 4; // specify flags from local field |
1056 | | // (unless I misread the spec) |
1057 | | // provide only modification time |
1058 | 0 | unsigned long time = (unsigned long)entry->date().toSecsSinceEpoch(); |
1059 | 0 | qToLittleEndian<quint32>(time, extfield + 5); |
1060 | 0 | extfield += 9; |
1061 | 0 | } |
1062 | |
|
1063 | 0 | if (entryNeedsZip64) { |
1064 | | // zip64 header |
1065 | 0 | extfield[0] = 0x01; |
1066 | 0 | extfield[1] = 0x00; |
1067 | | |
1068 | | // zip64 field size |
1069 | 0 | qToLittleEndian<quint16>(24, extfield + 2); |
1070 | | |
1071 | | // uncompressed file size |
1072 | 0 | qToLittleEndian<quint64>(size, extfield + 4); |
1073 | | |
1074 | | // compressed file size |
1075 | 0 | qToLittleEndian<quint64>(compressedSize, extfield + 12); |
1076 | | |
1077 | | // relative offset of local header |
1078 | 0 | qToLittleEndian<quint64>(myhst, extfield + 20); |
1079 | | |
1080 | | // number of the disk on which this file starts |
1081 | | // qToLittleEndian<quint32>(0, extfield + 28); |
1082 | 0 | } |
1083 | |
|
1084 | 0 | crc = crc32(crc, (Bytef *)buffer, bufferSize); |
1085 | 0 | bool ok = (device()->write(buffer, bufferSize) == bufferSize); |
1086 | 0 | delete[] buffer; |
1087 | 0 | if (!ok) { |
1088 | 0 | setErrorString(tr("Could not write file header: %1").arg(device()->errorString())); |
1089 | 0 | return false; |
1090 | 0 | } |
1091 | 0 | } |
1092 | 0 | qint64 centraldirendoffset = device()->pos(); |
1093 | 0 | qint64 cdsize = centraldirendoffset - centraldiroffset; |
1094 | | // total number of entries in central dir |
1095 | 0 | qint64 count = d->m_fileList.count(); |
1096 | |
|
1097 | 0 | bool archiveNeedsZip64 = (count >= 0xFFFF || cdsize >= 0xFFFFFFFF || centraldiroffset >= 0xFFFFFFFF); |
1098 | |
|
1099 | 0 | if (archiveNeedsZip64) { |
1100 | | // Write Zip64 End of Central Directory Record (PK\6\6) |
1101 | 0 | char zip64Eocd[56]; |
1102 | 0 | zip64Eocd[0] = 'P'; |
1103 | 0 | zip64Eocd[1] = 'K'; |
1104 | 0 | zip64Eocd[2] = 6; |
1105 | 0 | zip64Eocd[3] = 6; |
1106 | | |
1107 | | // size of the EOCD64 minus 12 |
1108 | 0 | quint64 eocdSize = 56 - 12; |
1109 | 0 | qToLittleEndian<quint64>(eocdSize, zip64Eocd + 4); |
1110 | | |
1111 | | // Version made by |
1112 | 0 | qToLittleEndian<quint16>(45, zip64Eocd + 12); |
1113 | | // Version needed to extract |
1114 | 0 | qToLittleEndian<quint16>(45, zip64Eocd + 14); |
1115 | | // Number of this disk |
1116 | 0 | qToLittleEndian<quint32>(0, zip64Eocd + 16); |
1117 | | // Disk where central directory starts |
1118 | 0 | qToLittleEndian<quint32>(0, zip64Eocd + 20); |
1119 | | // Number of central directory records on this disk |
1120 | 0 | qToLittleEndian<quint64>(count, zip64Eocd + 24); |
1121 | | // Total number of central directory records |
1122 | 0 | qToLittleEndian<quint64>(count, zip64Eocd + 32); |
1123 | | // Size of central directory in bytes |
1124 | 0 | qToLittleEndian<quint64>(cdsize, zip64Eocd + 40); |
1125 | | // Offset of start of central directory, relative to start of archive |
1126 | 0 | qToLittleEndian<quint64>(centraldiroffset, zip64Eocd + 48); |
1127 | | // Comment (up to the size of EOCD64) |
1128 | | // no comment |
1129 | |
|
1130 | 0 | if (device()->write(zip64Eocd, 56) != 56) { |
1131 | 0 | setErrorString(tr("Could not write Zip64 EOCD record: %1").arg(device()->errorString())); |
1132 | 0 | return false; |
1133 | 0 | } |
1134 | | |
1135 | | // Write Zip64 End of Central Directory Locator (PK\6\7) |
1136 | 0 | char zip64EocdLocator[20]; |
1137 | 0 | zip64EocdLocator[0] = 'P'; |
1138 | 0 | zip64EocdLocator[1] = 'K'; |
1139 | 0 | zip64EocdLocator[2] = 6; |
1140 | 0 | zip64EocdLocator[3] = 7; |
1141 | | |
1142 | | // Disk where EOCD64 starts |
1143 | 0 | qToLittleEndian<quint32>(0, zip64EocdLocator + 4); |
1144 | | // Offset to start of EOCD64, relative to start of archive |
1145 | 0 | qToLittleEndian<quint64>(centraldirendoffset, zip64EocdLocator + 8); |
1146 | | // Total number of disks |
1147 | 0 | qToLittleEndian<quint32>(1, zip64EocdLocator + 16); |
1148 | |
|
1149 | 0 | if (device()->write(zip64EocdLocator, 20) != 20) { |
1150 | 0 | setErrorString(tr("Could not write Zip64 EOCD locator: %1").arg(device()->errorString())); |
1151 | 0 | return false; |
1152 | 0 | } |
1153 | 0 | } |
1154 | | |
1155 | | // qCDebug(KArchiveLog) << "closearchive: centraldirendoffset: " << centraldirendoffset; |
1156 | | // qCDebug(KArchiveLog) << "closearchive: device()->pos(): " << device()->pos(); |
1157 | | |
1158 | | // write end of central dir record. |
1159 | 0 | char eocd[22]; |
1160 | 0 | eocd[0] = 'P'; // end of central dir signature |
1161 | 0 | eocd[1] = 'K'; |
1162 | 0 | eocd[2] = 5; |
1163 | 0 | eocd[3] = 6; |
1164 | | |
1165 | | // Number of this disk (or FF FF for ZIP64) |
1166 | 0 | if (archiveNeedsZip64) { |
1167 | 0 | qToLittleEndian<quint16>(0xFFFF, eocd + 4); |
1168 | 0 | } else { |
1169 | 0 | qToLittleEndian<quint16>(0, eocd + 4); |
1170 | 0 | } |
1171 | | // Disk where central directory starts (or FF FF for ZIP64) |
1172 | 0 | if (archiveNeedsZip64) { |
1173 | 0 | qToLittleEndian<quint16>(0xFFFF, eocd + 6); |
1174 | 0 | } else { |
1175 | 0 | qToLittleEndian<quint16>(0, eocd + 6); |
1176 | 0 | } |
1177 | | // Number of central directory records on this disk (or FF FF for ZIP64) |
1178 | 0 | if (archiveNeedsZip64) { |
1179 | 0 | qToLittleEndian<quint16>(0xFFFF, eocd + 8); |
1180 | 0 | } else { |
1181 | 0 | qToLittleEndian<quint16>(count, eocd + 8); |
1182 | 0 | } |
1183 | | // Total number of central directory records (or FF FF for ZIP64) |
1184 | 0 | if (archiveNeedsZip64) { |
1185 | 0 | qToLittleEndian<quint16>(0xFFFF, eocd + 10); |
1186 | 0 | } else { |
1187 | 0 | qToLittleEndian<quint16>(count, eocd + 10); |
1188 | 0 | } |
1189 | | // qCDebug(KArchiveLog) << "number of files (count): " << count; |
1190 | | |
1191 | | // Size of central directory in bytes (or FF FF FF FF for ZIP64) |
1192 | 0 | if (archiveNeedsZip64) { |
1193 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, eocd + 12); |
1194 | 0 | } else { |
1195 | 0 | qToLittleEndian<quint32>(cdsize, eocd + 12); |
1196 | 0 | } |
1197 | | // qCDebug(KArchiveLog) << "end : centraldirsize: " << cdsize; |
1198 | | |
1199 | | // Offset of start of central directory, relative to start of archive (or FF FF FF FF for ZIP64) |
1200 | 0 | if (archiveNeedsZip64) { |
1201 | 0 | qToLittleEndian<quint32>(0xFFFFFFFF, eocd + 16); |
1202 | 0 | } else { |
1203 | 0 | qToLittleEndian<quint32>(centraldiroffset, eocd + 16); |
1204 | 0 | } |
1205 | | // qCDebug(KArchiveLog) << "end : centraldiroffset: " << centraldiroffset; |
1206 | | |
1207 | | // Comment length |
1208 | 0 | qToLittleEndian<quint16>(0, eocd + 20); |
1209 | | // Comment (none) |
1210 | |
|
1211 | 0 | if (device()->write(eocd, 22) != 22) { |
1212 | 0 | setErrorString(tr("Could not write central dir record: %1").arg(device()->errorString())); |
1213 | 0 | return false; |
1214 | 0 | } |
1215 | | |
1216 | 0 | return true; |
1217 | 0 | } |
1218 | | |
1219 | | bool KZip::doWriteDir(const QString &name, |
1220 | | const QString &user, |
1221 | | const QString &group, |
1222 | | mode_t perm, |
1223 | | const QDateTime &atime, |
1224 | | const QDateTime &mtime, |
1225 | | const QDateTime &ctime) |
1226 | 0 | { |
1227 | | // Zip files have no explicit directories, they are implicitly created during extraction time |
1228 | | // when file entries have paths in them. |
1229 | | // However, to support empty directories, we must create a dummy file entry which ends with '/'. |
1230 | 0 | QString dirName = name; |
1231 | 0 | if (!name.endsWith(QLatin1Char('/'))) { |
1232 | 0 | dirName = dirName.append(QLatin1Char('/')); |
1233 | 0 | } |
1234 | 0 | return writeFile(dirName, QByteArrayView(), perm, user, group, atime, mtime, ctime); |
1235 | 0 | } |
1236 | | |
1237 | | bool KZip::doPrepareWriting(const QString &name, |
1238 | | const QString &user, |
1239 | | const QString &group, |
1240 | | qint64 size, |
1241 | | mode_t perm, |
1242 | | const QDateTime &accessTime, |
1243 | | const QDateTime &modificationTime, |
1244 | | const QDateTime &creationTime) |
1245 | 0 | { |
1246 | | // qCDebug(KArchiveLog); |
1247 | 0 | if (!isOpen()) { |
1248 | 0 | setErrorString(tr("Application error: ZIP file must be open before being written into")); |
1249 | 0 | qCWarning(KArchiveLog) << "doPrepareWriting failed: !isOpen()"; |
1250 | 0 | return false; |
1251 | 0 | } |
1252 | | |
1253 | 0 | if (!(mode() & QIODevice::WriteOnly)) { // accept WriteOnly and ReadWrite |
1254 | 0 | setErrorString(tr("Application error: attempted to write into non-writable ZIP file")); |
1255 | 0 | qCWarning(KArchiveLog) << "doPrepareWriting failed: !(mode() & QIODevice::WriteOnly)"; |
1256 | 0 | return false; |
1257 | 0 | } |
1258 | | |
1259 | 0 | if (!device()) { |
1260 | 0 | setErrorString(tr("Cannot create a device. Disk full?")); |
1261 | 0 | return false; |
1262 | 0 | } |
1263 | | |
1264 | | // set right offset in zip. |
1265 | 0 | if (!device()->seek(d->m_offset)) { |
1266 | 0 | setErrorString(tr("Cannot seek in ZIP file. Disk full?")); |
1267 | 0 | return false; |
1268 | 0 | } |
1269 | | |
1270 | 0 | uint atime = accessTime.toSecsSinceEpoch(); |
1271 | 0 | uint mtime = modificationTime.toSecsSinceEpoch(); |
1272 | 0 | uint ctime = creationTime.toSecsSinceEpoch(); |
1273 | | |
1274 | | // Find or create parent dir |
1275 | 0 | KArchiveDirectory *parentDir = rootDir(); |
1276 | 0 | QString fileName(name); |
1277 | 0 | int i = name.lastIndexOf(QLatin1Char('/')); |
1278 | 0 | if (i != -1) { |
1279 | 0 | QString dir = name.left(i); |
1280 | 0 | fileName = name.mid(i + 1); |
1281 | | // qCDebug(KArchiveLog) << "ensuring" << dir << "exists. fileName=" << fileName; |
1282 | 0 | parentDir = findOrCreate(dir); |
1283 | 0 | } |
1284 | | |
1285 | | // delete entries in the filelist with the same fileName as the one we want |
1286 | | // to save, so that we don't have duplicate file entries when viewing the zip |
1287 | | // with konqi... |
1288 | | // CAUTION: the old file itself is still in the zip and won't be removed !!! |
1289 | | // qCDebug(KArchiveLog) << "fileName to write: " << name; |
1290 | 0 | for (auto it = d->m_fileList.begin(); it != d->m_fileList.end();) { |
1291 | | // qCDebug(KArchiveLog) << "prepfileName: " << entry->path(); |
1292 | 0 | if (name == (*it)->path()) { |
1293 | | // also remove from the parentDir |
1294 | 0 | if (!parentDir->removeEntryV2(*it)) { |
1295 | 0 | return false; |
1296 | 0 | } |
1297 | | // qCDebug(KArchiveLog) << "removing following entry: " << entry->path(); |
1298 | 0 | delete *it; |
1299 | 0 | it = d->m_fileList.erase(it); |
1300 | 0 | } else { |
1301 | 0 | it++; |
1302 | 0 | } |
1303 | 0 | } |
1304 | | |
1305 | 0 | bool isZip64Archive = size >= 0xFFFFFFFF; |
1306 | |
|
1307 | 0 | int extra_field_len = 0; |
1308 | 0 | if (d->m_extraField == ModificationTime) { |
1309 | 0 | extra_field_len = 17; // value also used in finishWriting() |
1310 | 0 | } |
1311 | | // if isZip64Archive is true we write both sizes to the zip64 field |
1312 | 0 | if (isZip64Archive) { |
1313 | | // 2 header id + 2 field size + |
1314 | | // 8 uncompressed size + 8 compressed size |
1315 | 0 | extra_field_len += 20; |
1316 | 0 | } |
1317 | |
|
1318 | 0 | QByteArray encodedName = QFile::encodeName(name); |
1319 | | |
1320 | | // construct a KZipFileEntry and add it to list |
1321 | 0 | KZipFileEntry *e = new KZipFileEntry(this, |
1322 | 0 | fileName, |
1323 | 0 | perm, |
1324 | 0 | modificationTime, |
1325 | 0 | user, |
1326 | 0 | group, |
1327 | 0 | QString(), |
1328 | 0 | name, |
1329 | 0 | device()->pos() + 30 + encodedName.length() + extra_field_len, // start |
1330 | 0 | 0 /*size unknown yet*/, |
1331 | 0 | d->m_compression, |
1332 | 0 | 0 /*csize unknown yet*/); |
1333 | 0 | e->setHeaderStart(device()->pos()); |
1334 | | // qCDebug(KArchiveLog) << "wrote file start: " << e->position() << " name: " << name; |
1335 | 0 | if (!parentDir->addEntryV2(e)) { |
1336 | 0 | return false; |
1337 | 0 | } |
1338 | | |
1339 | 0 | d->m_currentFile = e; |
1340 | 0 | d->m_fileList.append(e); |
1341 | | |
1342 | | // write out zip header |
1343 | 0 | int bufferSize = extra_field_len + encodedName.length() + 30; |
1344 | | // qCDebug(KArchiveLog) << "bufferSize=" << bufferSize; |
1345 | 0 | char *buffer = new char[bufferSize]; |
1346 | |
|
1347 | 0 | buffer[0] = 'P'; // local file header signature |
1348 | 0 | buffer[1] = 'K'; |
1349 | 0 | buffer[2] = 3; |
1350 | 0 | buffer[3] = 4; |
1351 | | |
1352 | | // version needed to extract |
1353 | 0 | qToLittleEndian<quint16>(isZip64Archive ? 45 : 0x14, buffer + 4); |
1354 | | |
1355 | | // general purpose bit flag |
1356 | 0 | qToLittleEndian<quint16>(0, buffer + 6); |
1357 | | |
1358 | | // compression method |
1359 | 0 | qToLittleEndian<quint16>(char(e->encoding()), buffer + 8); |
1360 | |
|
1361 | 0 | transformToMsDos(e->date(), &buffer[10]); |
1362 | |
|
1363 | 0 | buffer[14] = 'C'; // dummy crc |
1364 | 0 | buffer[15] = 'R'; |
1365 | 0 | buffer[16] = 'C'; |
1366 | 0 | buffer[17] = 'q'; |
1367 | |
|
1368 | 0 | buffer[18] = 'C'; // compressed file size |
1369 | 0 | buffer[19] = 'S'; |
1370 | 0 | buffer[20] = 'I'; |
1371 | 0 | buffer[21] = 'Z'; |
1372 | |
|
1373 | 0 | buffer[22] = 'U'; // uncompressed file size |
1374 | 0 | buffer[23] = 'S'; |
1375 | 0 | buffer[24] = 'I'; |
1376 | 0 | buffer[25] = 'Z'; |
1377 | | |
1378 | | // fileName length |
1379 | 0 | qToLittleEndian<quint16>(encodedName.length(), buffer + 26); |
1380 | | |
1381 | | // extra field length |
1382 | 0 | qToLittleEndian<quint16>(extra_field_len, buffer + 28); |
1383 | | |
1384 | | // file name |
1385 | 0 | strncpy(buffer + 30, encodedName.constData(), encodedName.length()); |
1386 | | |
1387 | | // extra field |
1388 | 0 | char *extfield = buffer + 30 + encodedName.length(); |
1389 | 0 | if (d->m_extraField == ModificationTime) { |
1390 | | // "Extended timestamp" header (0x5455) |
1391 | 0 | extfield[0] = 'U'; |
1392 | 0 | extfield[1] = 'T'; |
1393 | 0 | extfield[2] = 13; // data size |
1394 | 0 | extfield[3] = 0; |
1395 | 0 | extfield[4] = 1 | 2 | 4; // contains mtime, atime, ctime |
1396 | |
|
1397 | 0 | qToLittleEndian<quint32>(mtime, extfield + 5); |
1398 | 0 | qToLittleEndian<quint32>(atime, extfield + 9); |
1399 | 0 | qToLittleEndian<quint32>(ctime, extfield + 13); |
1400 | 0 | extfield += 17; |
1401 | 0 | } |
1402 | |
|
1403 | 0 | if (isZip64Archive) { |
1404 | | // zip64 header |
1405 | 0 | extfield[0] = 0x01; |
1406 | 0 | extfield[1] = 0x00; |
1407 | | // size of uncompressed and compressed fields |
1408 | 0 | qToLittleEndian<quint16>(16, extfield + 2); |
1409 | | // uncompressed size |
1410 | 0 | qToLittleEndian<quint64>(size, extfield + 4); |
1411 | | // compressed size |
1412 | 0 | qToLittleEndian<quint64>(0, extfield + 12); |
1413 | 0 | } |
1414 | | |
1415 | | // Write header |
1416 | 0 | bool b = (device()->write(buffer, bufferSize) == bufferSize); |
1417 | 0 | d->m_crc = 0; |
1418 | 0 | delete[] buffer; |
1419 | |
|
1420 | 0 | if (!b) { |
1421 | 0 | setErrorString(tr("Could not write to the archive. Disk full?")); |
1422 | 0 | return false; |
1423 | 0 | } |
1424 | | |
1425 | | // Prepare device for writing the data |
1426 | | // Either device() if no compression, or a KCompressionDevice to compress |
1427 | 0 | if (d->m_compression == 0) { |
1428 | 0 | d->m_currentDev = device(); |
1429 | 0 | return true; |
1430 | 0 | } |
1431 | | |
1432 | 0 | auto compressionDevice = new KCompressionDevice(device(), false, KCompressionDevice::GZip); |
1433 | 0 | d->m_currentDev = compressionDevice; |
1434 | 0 | compressionDevice->setSkipHeaders(); // Just zlib, not gzip |
1435 | |
|
1436 | 0 | b = d->m_currentDev->open(QIODevice::WriteOnly); |
1437 | 0 | Q_ASSERT(b); |
1438 | |
|
1439 | 0 | if (!b) { |
1440 | 0 | setErrorString(tr("Could not open compression device: %1").arg(d->m_currentDev->errorString())); |
1441 | 0 | } |
1442 | |
|
1443 | 0 | return b; |
1444 | 0 | } |
1445 | | |
1446 | | bool KZip::doFinishWriting(qint64 size) |
1447 | 0 | { |
1448 | 0 | if (d->m_currentFile->encoding() == 8) { |
1449 | | // Finish |
1450 | 0 | (void)d->m_currentDev->write(nullptr, 0); |
1451 | 0 | delete d->m_currentDev; |
1452 | 0 | } |
1453 | | // If 0, d->m_currentDev was device() - don't delete ;) |
1454 | 0 | d->m_currentDev = nullptr; |
1455 | |
|
1456 | 0 | Q_ASSERT(d->m_currentFile); |
1457 | | // qCDebug(KArchiveLog) << "fileName: " << d->m_currentFile->path(); |
1458 | | // qCDebug(KArchiveLog) << "getpos (at): " << device()->pos(); |
1459 | 0 | d->m_currentFile->setSize(size); |
1460 | |
|
1461 | 0 | const QByteArray encodedName = QFile::encodeName(d->m_currentFile->path()); |
1462 | 0 | qint64 csize = device()->pos() - d->m_currentFile->position(); |
1463 | 0 | d->m_currentFile->setCompressedSize(csize); |
1464 | | // qCDebug(KArchiveLog) << "usize: " << d->m_currentFile->size(); |
1465 | | // qCDebug(KArchiveLog) << "csize: " << d->m_currentFile->compressedSize(); |
1466 | | // qCDebug(KArchiveLog) << "headerstart: " << d->m_currentFile->headerStart(); |
1467 | | |
1468 | | // qCDebug(KArchiveLog) << "crc: " << d->m_crc; |
1469 | 0 | d->m_currentFile->setCRC32(d->m_crc); |
1470 | |
|
1471 | 0 | d->m_currentFile = nullptr; |
1472 | | |
1473 | | // update saved offset for appending new files |
1474 | 0 | d->m_offset = device()->pos(); |
1475 | 0 | return true; |
1476 | 0 | } |
1477 | | |
1478 | | bool KZip::doWriteSymLink(const QString &name, |
1479 | | const QString &target, |
1480 | | const QString &user, |
1481 | | const QString &group, |
1482 | | mode_t perm, |
1483 | | const QDateTime &atime, |
1484 | | const QDateTime &mtime, |
1485 | | const QDateTime &ctime) |
1486 | 0 | { |
1487 | | // reassure that symlink flag is set, otherwise strange things happen on |
1488 | | // extraction |
1489 | 0 | perm |= QT_STAT_LNK; |
1490 | 0 | Compression c = compression(); |
1491 | 0 | setCompression(NoCompression); // link targets are never compressed |
1492 | |
|
1493 | 0 | if (!doPrepareWriting(name, user, group, 0, perm, atime, mtime, ctime)) { |
1494 | 0 | setCompression(c); |
1495 | 0 | return false; |
1496 | 0 | } |
1497 | | |
1498 | 0 | QByteArray symlink_target = QFile::encodeName(target); |
1499 | 0 | if (!writeData(symlink_target.constData(), symlink_target.length())) { |
1500 | 0 | setCompression(c); |
1501 | 0 | return false; |
1502 | 0 | } |
1503 | | |
1504 | 0 | if (!finishWriting(symlink_target.length())) { |
1505 | 0 | setCompression(c); |
1506 | 0 | return false; |
1507 | 0 | } |
1508 | | |
1509 | 0 | setCompression(c); |
1510 | 0 | return true; |
1511 | 0 | } |
1512 | | |
1513 | | void KZip::virtual_hook(int id, void *data) |
1514 | 0 | { |
1515 | 0 | KArchive::virtual_hook(id, data); |
1516 | 0 | } |
1517 | | |
1518 | | bool KZip::doWriteData(const char *data, qint64 size) |
1519 | 0 | { |
1520 | 0 | Q_ASSERT(d->m_currentFile); |
1521 | 0 | Q_ASSERT(d->m_currentDev); |
1522 | 0 | if (!d->m_currentFile || !d->m_currentDev) { |
1523 | 0 | setErrorString(tr("No file or device")); |
1524 | 0 | return false; |
1525 | 0 | } |
1526 | | |
1527 | | // crc to be calculated over uncompressed stuff... |
1528 | | // and they didn't mention it in their docs... |
1529 | 0 | quint32 crc = d->m_crc; |
1530 | |
|
1531 | 0 | constexpr qint64 chunkSize = 2 * 1024 * 1024; |
1532 | 0 | qint64 totalWritten = 0; |
1533 | 0 | while (totalWritten < size) { |
1534 | 0 | qint64 chunk = std::min(chunkSize, size - totalWritten); |
1535 | |
|
1536 | 0 | crc = crc32(crc, reinterpret_cast<const Bytef *>(data + totalWritten), static_cast<quint32>(chunk)); |
1537 | |
|
1538 | 0 | qint64 currentWritten = d->m_currentDev->write(data + totalWritten, chunk); |
1539 | 0 | if (currentWritten != chunk) { |
1540 | 0 | setErrorString(tr("Error writing data chunk: %1").arg(d->m_currentDev->errorString())); |
1541 | 0 | return false; |
1542 | 0 | } |
1543 | | |
1544 | 0 | totalWritten += currentWritten; |
1545 | 0 | } |
1546 | | |
1547 | 0 | d->m_crc = crc; |
1548 | | |
1549 | | // qCDebug(KArchiveLog) << "wrote" << size << "bytes."; |
1550 | 0 | const bool ok = totalWritten == size; |
1551 | |
|
1552 | 0 | if (!ok) { |
1553 | 0 | setErrorString(tr("Error writing data: %1").arg(d->m_currentDev->errorString())); |
1554 | 0 | } |
1555 | |
|
1556 | 0 | return ok; |
1557 | 0 | } |
1558 | | |
1559 | | void KZip::setCompression(Compression c) |
1560 | 0 | { |
1561 | 0 | d->m_compression = (c == NoCompression) ? 0 : 8; |
1562 | 0 | } |
1563 | | |
1564 | | KZip::Compression KZip::compression() const |
1565 | 0 | { |
1566 | 0 | return (d->m_compression == 8) ? DeflateCompression : NoCompression; |
1567 | 0 | } |
1568 | | |
1569 | | void KZip::setExtraField(ExtraField ef) |
1570 | 0 | { |
1571 | 0 | d->m_extraField = ef; |
1572 | 0 | } |
1573 | | |
1574 | | KZip::ExtraField KZip::extraField() const |
1575 | 0 | { |
1576 | 0 | return d->m_extraField; |
1577 | 0 | } |
1578 | | |
1579 | | //////////////////////////////////////////////////////////////////////// |
1580 | | ////////////////////// KZipFileEntry//////////////////////////////////// |
1581 | | //////////////////////////////////////////////////////////////////////// |
1582 | | class Q_DECL_HIDDEN KZipFileEntry::KZipFileEntryPrivate |
1583 | | { |
1584 | | public: |
1585 | | KZipFileEntryPrivate() |
1586 | 323k | : crc(0) |
1587 | 323k | , compressedSize(0) |
1588 | 323k | , headerStart(0) |
1589 | 323k | , encoding(0) |
1590 | 323k | { |
1591 | 323k | } |
1592 | | unsigned long crc; |
1593 | | qint64 compressedSize; |
1594 | | qint64 headerStart; |
1595 | | int encoding; |
1596 | | QString path; |
1597 | | }; |
1598 | | |
1599 | | KZipFileEntry::KZipFileEntry(KZip *zip, |
1600 | | const QString &name, |
1601 | | int access, |
1602 | | const QDateTime &date, |
1603 | | const QString &user, |
1604 | | const QString &group, |
1605 | | const QString &symlink, |
1606 | | const QString &path, |
1607 | | qint64 start, |
1608 | | qint64 uncompressedSize, |
1609 | | int encoding, |
1610 | | qint64 compressedSize) |
1611 | 323k | : KArchiveFile(zip, name, access, date, user, group, symlink, start, uncompressedSize) |
1612 | 323k | , d(new KZipFileEntryPrivate) |
1613 | 323k | { |
1614 | 323k | d->path = path; |
1615 | 323k | d->encoding = encoding; |
1616 | 323k | d->compressedSize = compressedSize; |
1617 | 323k | } |
1618 | | |
1619 | | KZipFileEntry::~KZipFileEntry() |
1620 | 323k | { |
1621 | 323k | delete d; |
1622 | 323k | } |
1623 | | |
1624 | | int KZipFileEntry::encoding() const |
1625 | 92.0k | { |
1626 | 92.0k | return d->encoding; |
1627 | 92.0k | } |
1628 | | |
1629 | | qint64 KZipFileEntry::compressedSize() const |
1630 | 143k | { |
1631 | 143k | return d->compressedSize; |
1632 | 143k | } |
1633 | | |
1634 | | void KZipFileEntry::setCompressedSize(qint64 compressedSize) |
1635 | 0 | { |
1636 | 0 | d->compressedSize = compressedSize; |
1637 | 0 | } |
1638 | | |
1639 | | void KZipFileEntry::setHeaderStart(qint64 headerstart) |
1640 | 323k | { |
1641 | 323k | d->headerStart = headerstart; |
1642 | 323k | } |
1643 | | |
1644 | | qint64 KZipFileEntry::headerStart() const |
1645 | 0 | { |
1646 | 0 | return d->headerStart; |
1647 | 0 | } |
1648 | | |
1649 | | unsigned long KZipFileEntry::crc32() const |
1650 | 0 | { |
1651 | 0 | return d->crc; |
1652 | 0 | } |
1653 | | |
1654 | | void KZipFileEntry::setCRC32(unsigned long crc32) |
1655 | 323k | { |
1656 | 323k | d->crc = crc32; |
1657 | 323k | } |
1658 | | |
1659 | | const QString &KZipFileEntry::path() const |
1660 | 20 | { |
1661 | 20 | return d->path; |
1662 | 20 | } |
1663 | | |
1664 | | QByteArray KZipFileEntry::data() const |
1665 | 30.2k | { |
1666 | 30.2k | QIODevice *dev = createDevice(); |
1667 | 30.2k | QByteArray arr; |
1668 | 30.2k | if (dev) { |
1669 | 29.7k | const qint64 devSize = dev->size(); |
1670 | 29.7k | if (devSize > kMaxQByteArraySize) { |
1671 | 191 | qCWarning(KArchiveLog) << "KZipFileEntry::data: Failed to allocate memory for file of size" << devSize; |
1672 | 191 | delete dev; |
1673 | 191 | return {}; |
1674 | 191 | } |
1675 | 29.5k | arr = dev->readAll(); |
1676 | 29.5k | delete dev; |
1677 | 29.5k | } |
1678 | 30.0k | return arr; |
1679 | 30.2k | } |
1680 | | |
1681 | | QIODevice *KZipFileEntry::createDevice() const |
1682 | 51.7k | { |
1683 | 51.7k | if (compressedSize() < 0) { |
1684 | 20 | qCWarning(KArchiveLog) << "KZipFileEntry::createDevice: Entry with negative size" << path() << compressedSize(); |
1685 | 20 | return nullptr; |
1686 | 20 | } |
1687 | | // qCDebug(KArchiveLog) << "creating iodevice limited to pos=" << position() << ", csize=" << compressedSize(); |
1688 | | // Limit the reading to the appropriate part of the underlying device (e.g. file) |
1689 | 51.6k | std::unique_ptr limitedDev = std::make_unique<KLimitedIODevice>(archive()->device(), position(), compressedSize()); |
1690 | 51.6k | if (encoding() == 0 || compressedSize() == 0) { // no compression (or even no data) |
1691 | 12.0k | return limitedDev.release(); |
1692 | 12.0k | } |
1693 | | |
1694 | 39.6k | if (encoding() == 8) { |
1695 | | // On top of that, create a device that uncompresses the zlib data |
1696 | 38.9k | KCompressionDevice *filterDev = new KCompressionDevice(std::move(limitedDev), KCompressionDevice::GZip, size()); |
1697 | | |
1698 | 38.9k | if (!filterDev) { |
1699 | 0 | return nullptr; // ouch |
1700 | 0 | } |
1701 | 38.9k | filterDev->setSkipHeaders(); // Just zlib, not gzip |
1702 | 38.9k | bool b = filterDev->open(QIODevice::ReadOnly); |
1703 | 38.9k | Q_UNUSED(b); |
1704 | 38.9k | Q_ASSERT(b); |
1705 | 38.9k | return filterDev; |
1706 | 38.9k | } |
1707 | | |
1708 | 39.6k | qCCritical(KArchiveLog) << "This zip file contains files compressed with method" << encoding() << ", this method is currently not supported by KZip," |
1709 | 685 | << "please use a command-line tool to handle this file."; |
1710 | 685 | return nullptr; |
1711 | 39.6k | } |