Overview
Comment: | OFZIPArchive: Add write and append support |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
4594f434e71fe3cb31f9eda514eea376 |
User & Date: | js on 2017-08-06 19:40:19 |
Other Links: | manifest | tags |
Context
2017-08-06
| ||
20:06 | OFZIPArchive: Improve signedness & range checks check-in: e86d11f124 user: js tags: trunk | |
19:40 | OFZIPArchive: Add write and append support check-in: 4594f434e7 user: js tags: trunk | |
2017-08-05
| ||
21:50 | OFFileManager: s/\(modification\)Time/\1Date/ check-in: 9c3423636f user: js tags: trunk | |
Changes
Modified src/OFMutableZIPArchiveEntry.h from [051d102b30] to [4a738fdf41].
︙ | |||
36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | + + | * The comment of the entry's file. */ @property OF_NULLABLE_PROPERTY (readwrite, nonatomic, copy) OFString *fileComment; /*! * The extra field of the entry. * * The item size *must* be 1! */ @property OF_NULLABLE_PROPERTY (readwrite, nonatomic, copy) OFData *extraField; /*! * The version which made the entry. * * The lower 8 bits are the ZIP specification version.@n |
︙ |
Modified src/OFMutableZIPArchiveEntry.m from [9090ae09d9] to [fd0258d247].
︙ | |||
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 | 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 | + + + + + + - + + + + + + + + + - + + + + + + + + + - + + + + + + + + + + + | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFMutableZIPArchiveEntry.h" #import "OFZIPArchiveEntry+Private.h" #import "OFString.h" #import "OFData.h" #import "OFDate.h" #import "OFInvalidArgumentException.h" #import "OFOutOfRangeException.h" @implementation OFMutableZIPArchiveEntry @dynamic fileName, fileComment, extraField, versionMadeBy, minVersionNeeded; @dynamic modificationDate, compressionMethod, compressedSize, uncompressedSize; @dynamic CRC32, versionSpecificAttributes, generalPurposeBitFlag; @dynamic of_localFileHeaderOffset; - copy { OFMutableZIPArchiveEntry *copy = [self mutableCopy]; [copy makeImmutable]; return copy; } - (void)setFileName: (OFString *)fileName { void *pool = objc_autoreleasePoolPush(); |
︙ | |||
104 105 106 107 108 109 110 111 112 113 114 115 116 | 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | + + + + + + + + | _versionSpecificAttributes = versionSpecificAttributes; } - (void)setGeneralPurposeBitFlag: (uint16_t)generalPurposeBitFlag { _generalPurposeBitFlag = generalPurposeBitFlag; } - (void)of_setLocalFileHeaderOffset: (int64_t)localFileHeaderOffset { if (localFileHeaderOffset < 0) @throw [OFInvalidArgumentException exception]; _localFileHeaderOffset = localFileHeaderOffset; } - (void)makeImmutable { object_setClass(self, [OFZIPArchiveEntry class]); } @end |
Modified src/OFTarArchive.h from [0314df0a93] to [166b823ec4].
︙ | |||
94 95 96 97 98 99 100 | 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 | - + + + + + | mode: (OFString *)mode; #endif /*! * @brief Returns the next entry from the tar archive or `nil` if all entries * have been read. * |
︙ |
Modified src/OFTarArchive.m from [4feff55415] to [3df34606f0].
︙ | |||
38 39 40 41 42 43 44 | 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 | + - + - + - + - | { OFTarArchiveEntry *_entry; OFStream *_stream; uint64_t _toRead; bool _atEndOfStream; } - initWithStream: (OFStream *)stream |
︙ | |||
208 209 210 211 212 213 214 | 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | - - + + | return nil; } entry = [[[OFTarArchiveEntry alloc] of_initWithHeader: buffer.c] autorelease]; _lastReturnedStream = [[OFTarArchive_FileReadStream alloc] |
︙ | |||
281 282 283 284 285 286 287 | 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | - - + + | stringToBuffer(buffer + 148, [OFString stringWithFormat: @"%06" PRIo16, checksum], 7); [_stream writeBuffer: buffer length: sizeof(buffer)]; _lastReturnedStream = [[OFTarArchive_FileWriteStream alloc] |
︙ | |||
312 313 314 315 316 317 318 | 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | + - + - | [_stream release]; _stream = nil; } @end @implementation OFTarArchive_FileReadStream - initWithStream: (OFStream *)stream |
︙ | |||
415 416 417 418 419 420 421 | 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | + - + - | if (size % 512 != 0) [_stream readIntoBuffer: buffer exactLength: 512 - ((size_t)size % 512)]; } @end @implementation OFTarArchive_FileWriteStream - initWithStream: (OFStream *)stream |
︙ |
Modified src/OFZIPArchive.h from [b0cecbd912] to [d9e0114a00].
︙ | |||
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 | 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 | + - + | * @class OFZIPArchive OFZIPArchive.h ObjFW/OFZIPArchive.h * * @brief A class for accessing and manipulating ZIP files. */ @interface OFZIPArchive: OFObject { OF_KINDOF(OFStream *) _stream; int64_t _offset; enum { OF_ZIP_ARCHIVE_MODE_READ, OF_ZIP_ARCHIVE_MODE_WRITE, OF_ZIP_ARCHIVE_MODE_APPEND } _mode; uint32_t _diskNumber, _centralDirectoryDisk; uint64_t _centralDirectoryEntriesInDisk, _centralDirectoryEntries; uint64_t _centralDirectorySize; int64_t _centralDirectoryOffset; OFString *_archiveComment; OFMutableArray OF_GENERIC(OFZIPArchiveEntry *) *_entries; OFMutableDictionary OF_GENERIC(OFString *, OFZIPArchiveEntry *) *_pathToEntryMap; OFStream *_lastReturnedStream; } /*! * The archive comment. */ |
︙ | |||
123 124 125 126 127 128 129 | 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 | - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | * @return The entries of the central directory of the archive as an array */ - (OFArray OF_GENERIC(OFZIPArchiveEntry *) *)entries; /*! * @brief Returns a stream for reading the specified file from the archive. * |
Modified src/OFZIPArchive.m from [a7134be3c5] to [93d7047dfd].
︙ | |||
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 | 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 |
︙ | |||
160 161 162 163 164 165 166 167 168 | 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]; |
︙ | |||
312 313 314 315 316 317 318 | 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); |
︙ | |||
378 379 380 381 382 383 384 | 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]; } |
︙ | |||
471 472 473 474 475 476 477 | 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 |
︙ | |||
589 590 591 592 593 594 595 | 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 |
Modified src/OFZIPArchiveEntry+Private.h from [f1b3349a83] to [e5aade0c05].
︙ | |||
20 21 22 23 24 25 26 27 28 29 | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | + + + + + + | @interface OFZIPArchiveEntry () @property (readonly, nonatomic) uint16_t of_lastModifiedFileTime, of_lastModifiedFileDate; @property (readonly, nonatomic) int64_t of_localFileHeaderOffset; - (instancetype)of_initWithStream: (OFStream *)stream OF_METHOD_FAMILY(init); - (uint64_t)of_writeToStream: (OFStream *)stream; @end @interface OFMutableZIPArchiveEntry () @property (readwrite, nonatomic, setter=of_setLocalFileHeaderOffset:) int64_t of_localFileHeaderOffset; @end OF_ASSUME_NONNULL_END |
Modified src/OFZIPArchiveEntry.h from [3cb34f0e06] to [64f6fec60c].
︙ | |||
111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | + + | /*! * The comment of the entry's file. */ @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *fileComment; /*! * The extra field of the entry. * * The item size *must* be 1! */ @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFData *extraField; /*! * The version which made the entry. * * The lower 8 bits are the ZIP specification version.@n |
︙ |
Modified src/OFZIPArchiveEntry.m from [b22fa20c14] to [728d6fb127].
︙ | |||
23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | + | #import "OFString.h" #import "OFData.h" #import "OFDate.h" #import "OFStream.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" extern uint32_t of_zip_archive_read_field32(const uint8_t **, uint16_t *); extern uint64_t of_zip_archive_read_field64(const uint8_t **, uint16_t *); OFString * of_zip_archive_entry_version_to_string(uint16_t version) { |
︙ | |||
153 154 155 156 157 158 159 | 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | + + + + + - + + + | } - initWithFileName: (OFString *)fileName { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); if ([fileName UTF8StringLength] > UINT16_MAX) @throw [OFOutOfRangeException exception]; |
︙ | |||
225 226 227 228 229 230 231 | 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | - + | _localFileHeaderOffset = of_zip_archive_read_field64(&ZIP64, &ZIP64Size); if (_startDiskNumber == 0xFFFF) _startDiskNumber = of_zip_archive_read_field32( &ZIP64, &ZIP64Size); |
︙ | |||
277 278 279 280 281 282 283 284 285 286 287 288 289 290 | 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | + + | copy->_internalAttributes = _internalAttributes; copy->_versionSpecificAttributes = _versionSpecificAttributes; copy->_localFileHeaderOffset = _localFileHeaderOffset; } @catch (id e) { [copy release]; @throw e; } return copy; } - (OFString *)fileName { return _fileName; } |
︙ | |||
398 399 400 401 402 403 404 405 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | [ret retain]; objc_autoreleasePoolPop(pool); return [ret autorelease]; } - (uint64_t)of_writeToStream: (OFStream *)stream { void *pool; uint64_t size = 0; if (_compressedSize > UINT32_MAX || _uncompressedSize > UINT32_MAX || _localFileHeaderOffset > UINT32_MAX) @throw [OFOutOfRangeException exception]; pool = objc_autoreleasePoolPush(); [stream writeLittleEndianInt32: 0x02014B50]; [stream writeLittleEndianInt16: _versionMadeBy]; [stream writeLittleEndianInt16: _minVersionNeeded]; [stream writeLittleEndianInt16: _generalPurposeBitFlag]; [stream writeLittleEndianInt16: _compressionMethod]; [stream writeLittleEndianInt16: _lastModifiedFileTime]; [stream writeLittleEndianInt16: _lastModifiedFileDate]; [stream writeLittleEndianInt32: _CRC32]; [stream writeLittleEndianInt32: (uint32_t)_compressedSize]; [stream writeLittleEndianInt32: (uint32_t)_uncompressedSize]; [stream writeLittleEndianInt16: (uint16_t)[_fileName UTF8StringLength]]; [stream writeLittleEndianInt16: (uint16_t)[_extraField count]]; [stream writeLittleEndianInt16: (uint16_t)[_fileComment UTF8StringLength]]; [stream writeLittleEndianInt16: _startDiskNumber]; [stream writeLittleEndianInt16: _internalAttributes]; [stream writeLittleEndianInt32: _versionSpecificAttributes]; [stream writeLittleEndianInt32: (uint32_t)_localFileHeaderOffset]; size += (4 + (6 * 2) + (3 * 4) + (5 * 2) + (2 * 4)); [stream writeString: _fileName]; if (_extraField != nil) [stream writeData: _extraField]; if (_fileComment != nil) [stream writeString: _fileComment]; size += (uint64_t)[_fileName UTF8StringLength] + (uint64_t)[_extraField count] + (uint64_t)[_fileComment UTF8StringLength]; objc_autoreleasePoolPop(pool); return size; } @end |
Modified src/crc32.h from [c923eabd0c] to [6d648798fd].
︙ | |||
22 23 24 25 26 27 28 | 22 23 24 25 26 27 28 29 30 31 32 33 | - + | #endif #import "macros.h" #ifdef __cplusplus extern "C" { #endif |
Modified src/crc32.m from [1f7220abde] to [888c0b6538].
︙ | |||
17 18 19 20 21 22 23 | 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | - + | #include "config.h" #import "crc32.h" #define CRC32_MAGIC 0xEDB88320 uint32_t |