Differences From Artifact [a7134be3c5]:
- File
src/OFZIPArchive.m
— part of check-in
[c404c33cf1]
at
2017-08-05 18:00:22
on branch trunk
— OFZIPArchive: Don't require an OFSeekableStream
This is now only necessary for reading and appending. (user: js, size: 14579) [annotate] [blame] [check-ins using]
To Artifact [93d7047dfd]:
- File src/OFZIPArchive.m — part of check-in [4594f434e7] at 2017-08-06 19:40:19 on branch trunk — OFZIPArchive: Add write and append support (user: js, size: 21243) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
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 | #import "OFUnsupportedVersionException.h" /* * FIXME: Current limitations: * - Split archives are not supported. * - Write support is missing. * - Encrypted files cannot be read. */ @interface OFZIPArchive () - (void)of_readZIPInfo; - (void)of_readEntries; @end @interface OFZIPArchive_LocalFileHeader: OFObject { @public uint16_t _minVersionNeeded, _generalPurposeBitFlag, _compressionMethod; uint16_t _lastModifiedFileTime, _lastModifiedFileDate; uint32_t _CRC32; uint64_t _compressedSize, _uncompressedSize; OFString *_fileName; OFData *_extraField; } - initWithStream: (OFStream *)stream; - (bool)matchesEntry: (OFZIPArchiveEntry *)entry; @end | > > > | | > > > > > > > > > > > > > | 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 | #import "OFUnsupportedVersionException.h" /* * FIXME: Current limitations: * - Split archives are not supported. * - Write support is missing. * - Encrypted files cannot be read. * - No support for writing ZIP64 files. */ @interface OFZIPArchive () - (void)of_readZIPInfo; - (void)of_readEntries; - (void)of_closeLastReturnedStream; - (void)of_writeCentralDirectory; @end @interface OFZIPArchive_LocalFileHeader: OFObject { @public uint16_t _minVersionNeeded, _generalPurposeBitFlag, _compressionMethod; uint16_t _lastModifiedFileTime, _lastModifiedFileDate; uint32_t _CRC32; uint64_t _compressedSize, _uncompressedSize; OFString *_fileName; OFData *_extraField; } - initWithStream: (OFStream *)stream; - (bool)matchesEntry: (OFZIPArchiveEntry *)entry; @end @interface OFZIPArchive_FileReadStream: OFStream { OFStream *_stream, *_decompressedStream; OFZIPArchive_LocalFileHeader *_localFileHeader; bool _hasDataDescriptor; uint64_t _size; uint32_t _CRC32; bool _atEndOfStream; } - initWithStream: (OFStream *)stream localFileHeader: (OFZIPArchive_LocalFileHeader *)localFileHeader; @end @interface OFZIPArchive_FileWriteStream: OFStream { OFStream *_stream; uint32_t _CRC32; @public uint64_t _bytesWritten; OFMutableZIPArchiveEntry *_entry; } - initWithStream: (OFStream *)stream entry: (OFMutableZIPArchiveEntry *)entry; @end uint32_t of_zip_archive_read_field32(const uint8_t **data, uint16_t *size) { uint32_t field = 0; if (*size < 4) |
︙ | ︙ | |||
160 161 162 163 164 165 166 167 168 | - initWithStream: (OF_KINDOF(OFStream *))stream mode: (OFString *)mode { self = [super init]; @try { _stream = [stream retain]; | > > > > > > > > > > > | > < < > | < < > | < | > > > > > | > > > > > > | 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 | - initWithStream: (OF_KINDOF(OFStream *))stream mode: (OFString *)mode { self = [super init]; @try { if ([mode isEqual: @"r"]) _mode = OF_ZIP_ARCHIVE_MODE_READ; else if ([mode isEqual: @"w"]) _mode = OF_ZIP_ARCHIVE_MODE_WRITE; else if ([mode isEqual: @"a"]) _mode = OF_ZIP_ARCHIVE_MODE_APPEND; else @throw [OFInvalidArgumentException exception]; _stream = [stream retain]; _entries = [[OFMutableArray alloc] init]; _pathToEntryMap = [[OFMutableDictionary alloc] init]; if (_mode == OF_ZIP_ARCHIVE_MODE_READ || _mode == OF_ZIP_ARCHIVE_MODE_APPEND) { if (![stream isKindOfClass: [OFSeekableStream class]]) @throw [OFInvalidArgumentException exception]; [self of_readZIPInfo]; [self of_readEntries]; } if (_mode == OF_ZIP_ARCHIVE_MODE_APPEND) { _offset = _centralDirectoryOffset; seekOrThrowInvalidFormat(_stream, (of_offset_t)_offset, SEEK_SET); } } @catch (id e) { [self release]; @throw e; } return self; } #ifdef OF_HAVE_FILES - initWithPath: (OFString *)path mode: (OFString *)mode { OFFile *file; if ([mode isEqual: @"a"]) file = [[OFFile alloc] initWithPath: path mode: @"r+"]; else file = [[OFFile alloc] initWithPath: path mode: mode]; @try { self = [self initWithStream: file mode: mode]; } @finally { [file release]; } return self; } #endif - (void)dealloc { [self close]; [_stream release]; [_archiveComment release]; [_entries release]; [_pathToEntryMap release]; [_lastReturnedStream release]; [super dealloc]; |
︙ | ︙ | |||
312 313 314 315 316 317 318 | if (_centralDirectoryOffset < 0 || (of_offset_t)_centralDirectoryOffset != _centralDirectoryOffset) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat(_stream, (of_offset_t)_centralDirectoryOffset, SEEK_SET); | < < < < < < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | < < < | | 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 | if (_centralDirectoryOffset < 0 || (of_offset_t)_centralDirectoryOffset != _centralDirectoryOffset) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat(_stream, (of_offset_t)_centralDirectoryOffset, SEEK_SET); for (size_t i = 0; i < _centralDirectoryEntries; i++) { OFZIPArchiveEntry *entry = [[[OFZIPArchiveEntry alloc] of_initWithStream: _stream] autorelease]; if ([_pathToEntryMap objectForKey: [entry fileName]] != nil) @throw [OFInvalidFormatException exception]; [_entries addObject: entry]; [_pathToEntryMap setObject: entry forKey: [entry fileName]]; } objc_autoreleasePoolPop(pool); } - (OFArray *)entries { return [[_entries copy] autorelease]; } - (OFString *)archiveComment { return _archiveComment; } - (void)setArchiveComment: (OFString *)comment { void *pool = objc_autoreleasePoolPush(); OFString *old; if ([comment UTF8StringLength] > UINT16_MAX) @throw [OFOutOfRangeException exception]; old = _archiveComment; _archiveComment = [comment copy]; [old release]; objc_autoreleasePoolPop(pool); } - (void)of_closeLastReturnedStream { [_lastReturnedStream close]; if ((_mode == OF_ZIP_ARCHIVE_MODE_WRITE || _mode == OF_ZIP_ARCHIVE_MODE_APPEND) && [_lastReturnedStream isKindOfClass: [OFZIPArchive_FileWriteStream class]]) { OFZIPArchive_FileWriteStream *stream = (OFZIPArchive_FileWriteStream *)_lastReturnedStream; _offset += stream->_bytesWritten; if (stream->_entry != nil) { [_entries addObject: stream->_entry]; [_pathToEntryMap setObject: stream->_entry forKey: [stream->_entry fileName]]; } } [_lastReturnedStream release]; _lastReturnedStream = nil; } - (OFStream *)streamForReadingFile: (OFString *)path { void *pool = objc_autoreleasePoolPush(); OFZIPArchiveEntry *entry; OFZIPArchive_LocalFileHeader *localFileHeader; int64_t offset64; if (_mode != OF_ZIP_ARCHIVE_MODE_READ) @throw [OFInvalidArgumentException exception]; if ((entry = [_pathToEntryMap objectForKey: path]) == nil) @throw [OFOpenItemFailedException exceptionWithPath: path mode: @"r" errNo: ENOENT]; [self of_closeLastReturnedStream]; offset64 = [entry of_localFileHeaderOffset]; if (offset64 < 0 || (of_offset_t)offset64 != offset64) @throw [OFOutOfRangeException exception]; seekOrThrowInvalidFormat(_stream, (of_offset_t)offset64, SEEK_SET); localFileHeader = [[[OFZIPArchive_LocalFileHeader alloc] |
︙ | ︙ | |||
378 379 380 381 382 383 384 | (localFileHeader->_minVersionNeeded & 0xFF) / 10, (localFileHeader->_minVersionNeeded & 0xFF) % 10]; @throw [OFUnsupportedVersionException exceptionWithVersion: version]; } | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 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 | (localFileHeader->_minVersionNeeded & 0xFF) / 10, (localFileHeader->_minVersionNeeded & 0xFF) % 10]; @throw [OFUnsupportedVersionException exceptionWithVersion: version]; } _lastReturnedStream = [[OFZIPArchive_FileReadStream alloc] initWithStream: _stream localFileHeader: localFileHeader]; objc_autoreleasePoolPop(pool); return [[_lastReturnedStream retain] autorelease]; } - (OFStream *)streamForWritingEntry: (OFZIPArchiveEntry *)entry_ { /* TODO: Avoid data descriptor when _stream is an OFSeekableStream */ void *pool; OFMutableZIPArchiveEntry *entry; OFString *fileName; OFData *extraField; uint16_t fileNameLength, extraFieldLength; if (_mode != OF_ZIP_ARCHIVE_MODE_WRITE && _mode != OF_ZIP_ARCHIVE_MODE_APPEND) @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); entry = [[entry_ mutableCopy] autorelease]; if ([_pathToEntryMap objectForKey: [entry fileName]] != nil) @throw [OFOpenItemFailedException exceptionWithPath: [entry fileName] mode: @"w" errNo: EEXIST]; if ([entry compressionMethod] != OF_ZIP_ARCHIVE_ENTRY_COMPRESSION_METHOD_NONE) @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; [self of_closeLastReturnedStream]; if (_offset > UINT32_MAX) @throw [OFOutOfRangeException exception]; [entry setVersionMadeBy: ([entry versionMadeBy] & 0xFF00) | 45]; [entry setMinVersionNeeded: ([entry minVersionNeeded] & 0xFF00) | 45]; [entry setCompressedSize: 0]; [entry setUncompressedSize: 0]; [entry setCRC32: 0]; [entry setGeneralPurposeBitFlag: [entry generalPurposeBitFlag] | (1 << 3) | (1 << 11)]; [entry of_setLocalFileHeaderOffset: _offset]; [_stream writeLittleEndianInt32: 0x04034B50]; [_stream writeLittleEndianInt16: [entry minVersionNeeded]]; [_stream writeLittleEndianInt16: [entry generalPurposeBitFlag]]; [_stream writeLittleEndianInt16: [entry compressionMethod]]; [_stream writeLittleEndianInt16: [entry of_lastModifiedFileTime]]; [_stream writeLittleEndianInt16: [entry of_lastModifiedFileDate]]; /* We use the data descriptor */ [_stream writeLittleEndianInt32: 0]; [_stream writeLittleEndianInt32: 0]; [_stream writeLittleEndianInt32: 0]; _offset += 4 + (5 * 2) + (3 * 4); fileName = [entry fileName]; fileNameLength = [fileName UTF8StringLength]; extraField = [entry extraField]; extraFieldLength = [extraField count]; [_stream writeLittleEndianInt16: fileNameLength]; [_stream writeLittleEndianInt16: extraFieldLength]; _offset += 2 * 2; [_stream writeString: fileName]; if (extraField != nil) [_stream writeData: extraField]; _offset += fileNameLength + extraFieldLength; _lastReturnedStream = [[OFZIPArchive_FileWriteStream alloc] initWithStream: _stream entry: entry]; objc_autoreleasePoolPop(pool); return [[_lastReturnedStream retain] autorelease]; } - (void)of_writeCentralDirectory { void *pool = objc_autoreleasePoolPush(); if (_offset > UINT32_MAX) @throw [OFOutOfRangeException exception]; _centralDirectoryEntries = 0; _centralDirectoryEntriesInDisk = 0; _centralDirectorySize = 0; _centralDirectoryOffset = _offset; for (OFZIPArchiveEntry *entry in _entries) { _centralDirectorySize += [entry of_writeToStream: _stream]; _centralDirectoryEntries++; _centralDirectoryEntriesInDisk++; } if (_centralDirectorySize > UINT32_MAX) @throw [OFOutOfRangeException exception]; [_stream writeLittleEndianInt32: 0x06054B50]; [_stream writeLittleEndianInt16: _diskNumber]; [_stream writeLittleEndianInt16: _centralDirectoryDisk]; [_stream writeLittleEndianInt16: _centralDirectoryEntriesInDisk]; [_stream writeLittleEndianInt16: _centralDirectoryEntries]; [_stream writeLittleEndianInt32: (uint32_t)_centralDirectorySize]; [_stream writeLittleEndianInt32: (uint32_t)_centralDirectoryOffset]; [_stream writeLittleEndianInt16: [_archiveComment UTF8StringLength]]; if (_archiveComment != nil) [_stream writeString: _archiveComment]; objc_autoreleasePoolPop(pool); } - (void)close { if (_stream == nil) return; [self of_closeLastReturnedStream]; if (_mode == OF_ZIP_ARCHIVE_MODE_WRITE || _mode == OF_ZIP_ARCHIVE_MODE_APPEND) [self of_writeCentralDirectory]; [_stream release]; _stream = nil; } @end @implementation OFZIPArchive_LocalFileHeader - initWithStream: (OFStream *)stream { self = [super init]; |
︙ | ︙ | |||
471 472 473 474 475 476 477 | if (![_fileName isEqual: [entry fileName]]) return false; return true; } @end | | | 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | if (![_fileName isEqual: [entry fileName]]) return false; return true; } @end @implementation OFZIPArchive_FileReadStream - initWithStream: (OFStream *)stream localFileHeader: (OFZIPArchive_LocalFileHeader *)localFileHeader { self = [super init]; @try { _stream = [stream retain]; |
︙ | ︙ | |||
589 590 591 592 593 594 595 | { [_stream release]; _stream = nil; [super close]; } @end | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | { [_stream release]; _stream = nil; [super close]; } @end @implementation OFZIPArchive_FileWriteStream - initWithStream: (OFStream *)stream entry: (OFMutableZIPArchiveEntry *)entry { self = [super init]; _stream = [stream retain]; _entry = [entry retain]; _CRC32 = ~0; return self; } - (void)dealloc { [self close]; [_stream release]; [_entry release]; [super dealloc]; } - (void)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { if (length > INT64_MAX || INT64_MAX - _bytesWritten < length) @throw [OFOutOfRangeException exception]; [_stream writeBuffer: buffer length: length]; _bytesWritten += length; _CRC32 = of_crc32(_CRC32, buffer, length); } - (void)close { uint32_t bytesWritten; if (_stream == nil) return; if (_bytesWritten > UINT32_MAX) @throw [OFOutOfRangeException exception]; bytesWritten = (uint32_t)_bytesWritten; [_stream writeLittleEndianInt32: 0x08074B50]; [_stream writeLittleEndianInt32: _CRC32]; [_stream writeLittleEndianInt32: bytesWritten]; [_stream writeLittleEndianInt32: bytesWritten]; _bytesWritten += (4 * 4); [_stream release]; _stream = nil; [_entry setCRC32: ~_CRC32]; [_entry setCompressedSize: bytesWritten]; [_entry setUncompressedSize: bytesWritten]; [_entry makeImmutable]; } @end |