/
ziparchive.cpp
601 lines (529 loc) · 18.4 KB
/
ziparchive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2004-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "de/ZipArchive"
#include "de/ISerializable"
#include "de/ByteSubArray"
#include "de/FixedByteArray"
#include "de/ByteArrayFile"
#include "de/Reader"
#include "de/Writer"
#include "de/LittleEndianByteOrder"
#include "de/Block"
#include "de/File"
#include "de/Date"
#include "de/Zeroed"
#include <cstring>
#include <zlib.h>
namespace de {
namespace internal {
// Marker signatures.
#define SIG_LOCAL_FILE_HEADER 0x04034b50
#define SIG_CENTRAL_FILE_HEADER 0x02014b50
#define SIG_END_OF_CENTRAL_DIR 0x06054b50
#define SIG_DIGITAL_SIGNATURE 0x05054b50
// Maximum tolerated size of the comment.
#define MAXIMUM_COMMENT_SIZE 2048
// This is the length of the central directory end record (without the
// comment, but with the signature).
#define CENTRAL_END_SIZE 22
// Deflate minimum compression. Worse than this will be stored uncompressed.
#define REQUIRED_DEFLATE_PERCENTAGE .98
// File header flags.
#define ZFH_ENCRYPTED 0x1
#define ZFH_COMPRESSION_OPTS 0x6
#define ZFH_DESCRIPTOR 0x8
#define ZFH_COMPRESS_PATCHED 0x20 // Not supported.
// Compression methods.
enum Compression {
NO_COMPRESSION = 0, // Supported format.
SHRUNK,
REDUCED_1,
REDUCED_2,
REDUCED_3,
REDUCED_4,
IMPLODED,
DEFLATED = 8, // The only supported compression (via zlib).
DEFLATED_64,
PKWARE_DCL_IMPLODED
};
/**
* MS-DOS time.
* - 0..4: Second/2
* - 5..10: Minute
* - 11..15: Hour
*/
struct DOSTime {
duint16 seconds;
duint16 minutes;
duint16 hours;
DOSTime(duint16 h, duint16 m, duint16 s) : seconds(s), minutes(m), hours(h) {}
DOSTime(duint16 const &i) {
seconds = (i & 0x1f) * 2;
minutes = (i >> 5) & 0x3f;
hours = i >> 11;
}
operator duint16() const {
return (hours << 11) | ((minutes & 0x3f) << 5) | ((seconds/2) & 0x1f);
}
};
/**
* MS-DOS date.
* - 0..4: Day of the month (1-31)
* - 5..8: Month (1=Jan)
* - 9..15: Year since 1980
*/
struct DOSDate {
duint16 dayOfMonth;
duint16 month;
duint16 year;
DOSDate(duint16 y, duint16 m, duint16 d) : dayOfMonth(d), month(m), year(y) {}
DOSDate(duint16 const &i) {
dayOfMonth = i & 0x1f;
month = (i >> 5) & 0xf;
year = i >> 9;
}
operator duint16() const {
return (year << 9) | ((month & 0xf) << 5) | (dayOfMonth & 0x1f);
}
};
struct LocalFileHeader : public ISerializable {
Uint32 signature;
Uint16 requiredVersion;
Uint16 flags;
Uint16 compression;
Uint16 lastModTime;
Uint16 lastModDate;
Uint32 crc32;
Uint32 compressedSize;
Uint32 size;
Uint16 fileNameSize;
Uint16 extraFieldSize;
void operator >> (Writer &to) const {
to << signature
<< requiredVersion
<< flags
<< compression
<< lastModTime
<< lastModDate
<< crc32
<< compressedSize
<< size
<< fileNameSize
<< extraFieldSize;
}
void operator << (Reader &from) {
from >> signature
>> requiredVersion
>> flags
>> compression
>> lastModTime
>> lastModDate
>> crc32
>> compressedSize
>> size
>> fileNameSize
>> extraFieldSize;
}
};
struct CentralFileHeader : public ISerializable {
Uint32 signature;
Uint16 version;
Uint16 requiredVersion;
Uint16 flags;
Uint16 compression;
Uint16 lastModTime;
Uint16 lastModDate;
Uint32 crc32;
Uint32 compressedSize;
Uint32 size;
Uint16 fileNameSize;
Uint16 extraFieldSize;
Uint16 commentSize;
Uint16 diskStart;
Uint16 internalAttrib;
Uint32 externalAttrib;
Uint32 relOffset;
/*
* Followed by:
* file name (variable size)
* extra field (variable size)
* file comment (variable size)
*/
void operator >> (Writer &to) const {
to << signature
<< version
<< requiredVersion
<< flags
<< compression
<< lastModTime
<< lastModDate
<< crc32
<< compressedSize
<< size
<< fileNameSize
<< extraFieldSize
<< commentSize
<< diskStart
<< internalAttrib
<< externalAttrib
<< relOffset;
}
void operator << (Reader &from) {
from >> signature
>> version
>> requiredVersion
>> flags
>> compression
>> lastModTime
>> lastModDate
>> crc32
>> compressedSize
>> size
>> fileNameSize
>> extraFieldSize
>> commentSize
>> diskStart
>> internalAttrib
>> externalAttrib
>> relOffset;
}
};
struct CentralEnd : public ISerializable {
Uint16 disk;
Uint16 centralStartDisk;
Uint16 diskEntryCount;
Uint16 totalEntryCount;
Uint32 size;
Uint32 offset;
Uint16 commentSize;
void operator >> (Writer &to) const {
to << disk
<< centralStartDisk
<< diskEntryCount
<< totalEntryCount
<< size
<< offset
<< commentSize;
}
void operator << (Reader &from) {
from >> disk
>> centralStartDisk
>> diskEntryCount
>> totalEntryCount
>> size
>> offset
>> commentSize;
}
};
} // namespace internal
using namespace internal;
ZipArchive::ZipArchive() : Archive()
{}
ZipArchive::ZipArchive(IByteArray const &archive) : Archive(archive)
{
Reader reader(archive, littleEndianByteOrder);
// Locate the central directory. Start from the earliest location where
// the signature might be.
duint centralEndPos = 0;
for(duint pos = CENTRAL_END_SIZE; pos < MAXIMUM_COMMENT_SIZE; pos++)
{
reader.setOffset(archive.size() - pos);
duint32 signature;
reader >> signature;
if(signature == SIG_END_OF_CENTRAL_DIR)
{
// This is it!
centralEndPos = archive.size() - pos;
break;
}
}
if(!centralEndPos)
{
/// @throw MissingCentralDirectoryError The ZIP central directory was not found
/// in the end of the source data.
throw MissingCentralDirectoryError("ZipArchive::Archive",
"Could not locate the central directory of the archive");
}
// The central directory end record is at the signature we found.
CentralEnd summary;
reader >> summary;
duint const entryCount = summary.totalEntryCount;
// The ZIP must have only one part, all entries in the same archive.
if(entryCount != summary.diskEntryCount)
{
/// @throw MultiPartError ZIP archives in more than one part are not supported
/// by the implementation.
throw MultiPartError("ZipArchive::Archive", "Multipart archives are not supported");
}
// Read all the entries of the central directory.
reader.setOffset(summary.offset);
for(duint index = 0; index < entryCount; ++index)
{
CentralFileHeader header;
reader >> header;
// Check the signature.
if(header.signature != SIG_CENTRAL_FILE_HEADER)
{
/// @throw FormatError Invalid signature in a central directory entry.
throw FormatError("ZipArchive::Archive", "Corrupt central directory");
}
String fileName = String::fromLatin1(ByteSubArray(archive, reader.offset(), header.fileNameSize));
// Advance the cursor past the variable sized fields.
reader.seek(header.fileNameSize + header.extraFieldSize + header.commentSize);
// Skip folders.
if(fileName.endsWith("/") && !header.size)
{
continue;
}
// Check for unsupported features.
if(header.compression != NO_COMPRESSION && header.compression != DEFLATED)
{
/// @throw UnknownCompressionError Deflation is the only compression
/// algorithm supported by the implementation.
throw UnknownCompressionError("ZipArchive::Archive",
"Entry '" + fileName + "' uses an unsupported compression algorithm");
}
if(header.flags & ZFH_ENCRYPTED)
{
/// @throw EncryptionError Archive is encrypted, which is not supported
/// by the implementation.
throw EncryptionError("ZipArchive::Archive",
"Entry '" + fileName + "' is encrypted and thus cannot be read");
}
// Make an index entry for this.
QScopedPointer<ZipEntry> entry(new ZipEntry);
entry->size = header.size;
entry->sizeInArchive = header.compressedSize;
entry->compression = header.compression;
entry->crc32 = header.crc32;
entry->localHeaderOffset = header.relOffset;
// Unpack the last modified time from the ZIP entry header.
DOSDate lastModDate(header.lastModDate);
DOSTime lastModTime(header.lastModTime);
entry->modifiedAt = QDateTime(QDate(lastModDate.year + 1980, lastModDate.month, lastModDate.dayOfMonth),
QTime(lastModTime.hours, lastModTime.minutes, lastModTime.seconds));
// Read the local file header, which contains the correct extra
// field size (Info-ZIP!).
reader.mark();
reader.setOffset(header.relOffset);
LocalFileHeader localHeader;
reader >> localHeader;
entry->offset = reader.offset() + header.fileNameSize + localHeader.extraFieldSize;
insertToIndex(fileName, entry.take());
// Back to the central directory.
reader.rewind();
}
}
ZipArchive::~ZipArchive()
{}
void ZipArchive::readFromSource(Entry const *e, String const &, IBlock &uncompressedData) const
{
ZipEntry const &entry = *static_cast<ZipEntry const *>(e);
if(entry.compression == NO_COMPRESSION)
{
// Data is not compressed so we can just read it.
if(entry.dataInArchive)
{
uncompressedData.copyFrom(*entry.dataInArchive, 0, entry.size);
}
else
{
DENG2_ASSERT(source() != NULL);
uncompressedData.copyFrom(*source(), entry.offset, entry.size);
}
}
else // Data is compressed.
{
// Prepare the output buffer for the decompressed data.
uncompressedData.resize(entry.size);
// Take a copy of the compressed data for zlib.
if(!entry.dataInArchive)
{
DENG2_ASSERT(source() != NULL);
entry.dataInArchive = new Block(*source(), entry.offset, entry.sizeInArchive);
}
z_stream stream;
std::memset(&stream, 0, sizeof(stream));
stream.next_in = const_cast<IByteArray::Byte *>(entry.dataInArchive->data());
stream.avail_in = entry.sizeInArchive;
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.next_out = const_cast<IByteArray::Byte *>(uncompressedData.data());
stream.avail_out = entry.size;
if(inflateInit2(&stream, -MAX_WBITS) != Z_OK)
{
/// @throw InflateError Problem with zlib: inflateInit2 failed.
throw InflateError("ZipArchive::readEntry",
"Inflation failed because initialization failed");
}
// Do the inflation in one call.
dint result = inflate(&stream, Z_FINISH);
if(stream.total_out != entry.size)
{
/// @throw InflateError The actual decompressed size is not equal to the
/// size listed in the central directory.
throw InflateError("ZipArchive::readEntry",
"Failure due to " +
String((result == Z_DATA_ERROR ? "corrupt data in archive" :
"zlib error")) + ": " + stream.msg);
}
// We're done.
inflateEnd(&stream);
}
}
void ZipArchive::operator >> (Writer &to) const
{
/**
* ZIP archives will use little-endian byte order regardless of the byte
* order employed by the supplied Writer @a to.
*/
Writer writer(to, littleEndianByteOrder);
// First write the local headers.
DENG2_FOR_EACH_CONST(Index, i, index())
{
// We will be updating relevant members of the entry.
ZipEntry &entry = *static_cast<ZipEntry *>(const_cast<Entry *>(i.value()));
entry.update();
// This is where the local file header is located.
entry.localHeaderOffset = writer.offset();
LocalFileHeader header;
header.signature = SIG_LOCAL_FILE_HEADER;
header.requiredVersion = 20;
header.compression = entry.compression;
Date at(entry.modifiedAt);
header.lastModTime = DOSTime(at.hours(), at.minutes(), at.seconds());
header.lastModDate = DOSDate(at.year() - 1980, at.month(), at.dayOfMonth());
header.crc32 = entry.crc32;
header.compressedSize = entry.sizeInArchive;
header.size = entry.size;
header.fileNameSize = i.key().size();
// Can we use the data already in the source archive?
if((entry.dataInArchive || source()) && !entry.maybeChanged)
{
// Yes, we can.
writer << header << FixedByteArray(i.key().toLatin1());
IByteArray::Offset newOffset = writer.offset();
if(entry.dataInArchive)
{
writer << FixedByteArray(*entry.dataInArchive);
}
else
{
// Re-use the data in the source.
writer << FixedByteArray(*source(), entry.offset, entry.sizeInArchive);
}
// Written to new location.
entry.offset = newOffset;
}
else
{
DENG2_ASSERT(entry.data != NULL);
// Let's try and compress.
Block archived(Block::Size(REQUIRED_DEFLATE_PERCENTAGE * entry.data->size()));
z_stream stream;
std::memset(&stream, 0, sizeof(stream));
stream.next_in = const_cast<IByteArray::Byte *>(entry.data->data());
stream.avail_in = entry.data->size();
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.next_out = const_cast<IByteArray::Byte *>(archived.data());
stream.avail_out = archived.size();
if(deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
-MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK)
{
/// @throw DeflateError zlib error: could not initialize deflate operation.
throw DeflateError("ZipArchive::operator >>", "Deflate init failed");
}
deflate(&stream, Z_FINISH);
int result = deflateEnd(&stream);
if(result == Z_OK)
{
// Compression was ok.
header.compression = entry.compression = DEFLATED;
header.compressedSize = entry.sizeInArchive = stream.total_out;
writer << header << FixedByteArray(i.key().toLatin1());
entry.offset = writer.offset();
writer << FixedByteArray(archived, 0, entry.sizeInArchive);
}
else
{
// We won't compress.
header.compression = entry.compression = NO_COMPRESSION;
header.compressedSize = entry.sizeInArchive = entry.data->size();
writer << header << FixedByteArray(i.key().toLatin1());
entry.offset = writer.offset();
writer << FixedByteArray(*entry.data);
}
}
}
CentralEnd summary;
summary.diskEntryCount = summary.totalEntryCount = index().size();
// This is where the central directory begins.
summary.offset = writer.offset();
// Write the central directory.
DENG2_FOR_EACH_CONST(Index, i, index())
{
ZipEntry const &entry = *static_cast<ZipEntry const *>(i.value());
CentralFileHeader header;
header.signature = SIG_CENTRAL_FILE_HEADER;
header.version = 20;
header.requiredVersion = 20;
Date at(entry.modifiedAt);
header.lastModTime = DOSTime(at.hours(), at.minutes(), at.seconds());
header.lastModDate = DOSDate(at.year() - 1980, at.month(), at.dayOfMonth());
header.compression = entry.compression;
header.crc32 = entry.crc32;
header.compressedSize = entry.sizeInArchive;
header.size = entry.size;
header.fileNameSize = i.key().size();
header.relOffset = entry.localHeaderOffset;
writer << header << FixedByteArray(i.key().toLatin1());
}
// Size of the central directory.
summary.size = writer.offset() - summary.offset;
// End of central directory.
writer << duint32(SIG_END_OF_CENTRAL_DIR) << summary;
// No signature data.
writer << duint32(SIG_DIGITAL_SIGNATURE) << duint16(0);
// Since we used our own writer, seek the writer that was given to us
// by the amount of data we wrote.
to.seek(writer.offset());
}
bool ZipArchive::recognize(File const &file)
{
// For now, just check the name.
String ext = file.name().fileNameExtension().lower();
return (ext == ".pack" || ext == ".demo" || ext == ".save" || ext == ".addon" ||
ext == ".box" || ext == ".pk3" || ext == ".zip");
}
Archive::Entry *ZipArchive::newEntry()
{
ZipEntry *entry = new ZipEntry;
entry->compression = NO_COMPRESSION; // Will be updated.
return entry;
}
void ZipArchive::ZipEntry::update()
{
if(data)
{
size = data->size();
crc32 = ::crc32(0L, data->data(), data->size());
}
}
} // namespace de