Overview
Comment: | OFZooArchive: Add write support |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
b431203426d5b10938276ab9ebacecb3 |
User & Date: | js on 2024-03-03 19:29:53 |
Other Links: | manifest | tags |
Context
2024-03-03
| ||
19:38 | Add IRI handler for Zoo archives check-in: 4e1846598b user: js tags: trunk | |
19:29 | OFZooArchive: Add write support check-in: b431203426 user: js tags: trunk | |
2024-03-02
| ||
21:20 | Minor documentation fix check-in: 7619af113a user: js tags: trunk | |
Changes
Modified src/OFLHAArchive.m from [97efb45aaf] to [7fd2208cb9].
︙ | |||
294 295 296 297 298 299 300 | 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | - + | { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; @try { [_lastReturnedStream close]; } @catch (OFNotOpenException *e) { |
︙ |
Modified src/OFMutableZooArchiveEntry.m from [26fa9bdcde] to [5eea787dbf].
︙ | |||
35 36 37 38 39 40 41 | 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | - + | - (instancetype)initWithFileName: (OFString *)fileName { self = [super of_init]; @try { void *pool = objc_autoreleasePoolPush(); |
︙ | |||
125 126 127 128 129 130 131 | 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 | + + + - - - + + + + + + + + + + + + + + - - + + + + + | OFString *old = _fileComment; _fileComment = [fileComment copy]; [old release]; } - (void)setFileName: (OFString *)fileName { void *pool = objc_autoreleasePoolPush(); OFString *oldFileName = _fileName, *oldDirectoryName = _directoryName; size_t lastSlash; |
︙ |
Modified src/OFZooArchive.h from [2cebcb2c91] to [3db7e3fb48].
︙ | |||
10 11 12 13 14 15 16 17 18 19 20 21 | 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 | + - - + + + + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "OFSeekableStream.h" #import "OFString.h" #import "OFZooArchiveEntry.h" OF_ASSUME_NONNULL_BEGIN |
Modified src/OFZooArchive.m from [1d909708d8] to [02c033a0c2].
︙ | |||
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 | 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 | + + + + - + + | * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #define OF_ZOO_ARCHIVE_M #include "config.h" #include <errno.h> #import "OFZooArchive.h" #import "OFZooArchiveEntry.h" #import "OFZooArchiveEntry+Private.h" #import "OFCRC16.h" #import "OFIRI.h" #import "OFIRIHandler.h" #import "OFKernelEventObserver.h" #import "OFLHADecompressingStream.h" #import "OFSeekableStream.h" #import "OFStream.h" #import "OFString.h" #import "OFChecksumMismatchException.h" #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" #import "OFTruncatedDataException.h" #import "OFUnsupportedVersionException.h" #import "OFWriteFailedException.h" enum { |
︙ | |||
58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 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 | + + + + + + + + + + + + + + + + + + + + + | bool _atEndOfStream; } - (instancetype)of_initWithArchive: (OFZooArchive *)archive stream: (OFStream *)stream entry: (OFZooArchiveEntry *)entry; @end OF_DIRECT_MEMBERS @interface OFZooArchiveFileWriteStream: OFStream <OFReadyForWritingObserving> { OFZooArchive *_archive; OFMutableZooArchiveEntry *_entry; OFStringEncoding _encoding; OFSeekableStream *_stream; OFStreamOffset *_lastHeaderOffset; size_t *_lastHeaderLength; uint32_t _bytesWritten; uint16_t _CRC16; } - (instancetype)of_initWithArchive: (OFZooArchive *)archive stream: (OFSeekableStream *)stream entry: (OFZooArchiveEntry *)entry encoding: (OFStringEncoding)encoding lastHeaderOffset: (OFStreamOffset *)lastHeaderOffset lastHeaderLength: (size_t *)lastHeaderLength; @end @implementation OFZooArchive @synthesize encoding = _encoding; + (instancetype)archiveWithStream: (OFStream *)stream mode: (OFString *)mode { return [[[self alloc] initWithStream: stream mode: mode] autorelease]; |
︙ | |||
84 85 86 87 88 89 90 | 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 | - + + + + + + - + - - - - - - + + | - (instancetype)initWithStream: (OFStream *)stream mode: (OFString *)mode { self = [super init]; @try { if ([mode isEqual: @"r"]) _mode = modeRead; |
︙ | |||
155 156 157 158 159 160 161 | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | + + + - - + + + + + + | @throw [OFInvalidFormatException exception]; firstFileOffset = [_stream readLittleEndianInt32]; if ([_stream readLittleEndianInt32] != ~(uint32_t)(firstFileOffset - 1)) @throw [OFInvalidFormatException exception]; if ((_minVersionNeeded = [_stream readBigEndianInt16]) > 0x201) @throw [OFUnsupportedVersionException exceptionWithVersion: [OFString stringWithFormat: @"%" PRIu8 @".%" PRIu8, |
︙ | |||
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + | _lastReturnedStream = [[[OFZooArchiveFileReadStream alloc] of_initWithArchive: self stream: _stream entry: _currentEntry] autorelease]; return _lastReturnedStream; } - (void)of_fixUpLastHeader { OFStreamOffset offset; unsigned char *buffer; if (_lastHeaderOffset == 0) return; offset = [_stream seekToOffset: 0 whence: OFSeekCurrent]; if (offset < 0 || offset > UINT32_MAX || _lastHeaderLength < 56) @throw [OFOutOfRangeException exception]; [_stream seekToOffset: _lastHeaderOffset whence: OFSeekSet]; buffer = OFAllocMemory(1, _lastHeaderLength); @try { uint16_t tmp16; uint32_t tmp32; [_stream readIntoBuffer: buffer exactLength: _lastHeaderLength]; tmp32 = OFToLittleEndian32((uint32_t)offset); memcpy(buffer + 6, &tmp32, 4); tmp16 = OFToLittleEndian16( OFCRC16(0, buffer, _lastHeaderLength)); memcpy(buffer + 54, &tmp16, 2); [_stream seekToOffset: _lastHeaderOffset whence: OFSeekSet]; [_stream writeBuffer: buffer length: _lastHeaderLength]; [_stream seekToOffset: offset whence: OFSeekSet]; } @finally { OFFreeMemory(buffer); } } - (OFStream *)streamForWritingEntry: (OFZooArchiveEntry *)entry { if (_mode != modeWrite) @throw [OFInvalidArgumentException exception]; if (entry.compressionMethod != 0) @throw [OFNotImplementedException exceptionWithSelector: _cmd object: self]; if (_lastHeaderOffset == 0) { /* First file - write header. */ [_stream writeBuffer: "ObjFW Zoo Archive.\x1F" length: 20]; [_stream writeLittleEndianInt32: 0xFDC4A7DC]; [_stream writeLittleEndianInt32: 42]; [_stream writeLittleEndianInt32: -42]; /* TODO: Increase to 0x201 once we add compressed files. */ [_stream writeBigEndianInt16: 0x200]; /* Header type */ [_stream writeInt8: 1]; /* Archive comment offset */ [_stream writeLittleEndianInt32: 0]; /* Archive comment length */ [_stream writeLittleEndianInt16: 0]; /* Version flag */ [_stream writeInt8: 0]; } else [self of_fixUpLastHeader]; @try { [_lastReturnedStream close]; } @catch (OFNotOpenException *e) { /* Might have already been closed by the user - that's fine. */ } _lastReturnedStream = nil; _lastReturnedStream = [[[OFZooArchiveFileWriteStream alloc] of_initWithArchive: self stream: _stream entry: entry encoding: _encoding lastHeaderOffset: &_lastHeaderOffset lastHeaderLength: &_lastHeaderLength] autorelease]; return _lastReturnedStream; } - (void)close { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; @try { [_lastReturnedStream close]; } @catch (OFNotOpenException *e) { /* Might have already been closed by the user - that's fine. */ } _lastReturnedStream = nil; /* * Zoo archives should be terminated with an entry that has a next * header offset of 0. */ if (_mode == modeWrite) { static const unsigned char header[56] = { 0xDC, 0xA7, 0xC4, 0xFD, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFC, 0x83 }; |
︙ | |||
345 346 347 348 349 350 351 352 353 354 355 | 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 602 603 604 605 606 607 608 609 610 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | @throw [OFNotOpenException exceptionWithObject: self]; [_stream release]; _stream = nil; [_decompressedStream release]; _decompressedStream = nil; [super close]; } @end @implementation OFZooArchiveFileWriteStream - (instancetype)of_initWithArchive: (OFZooArchive *)archive stream: (OFSeekableStream *)stream entry: (OFZooArchiveEntry *)entry encoding: (OFStringEncoding)encoding lastHeaderOffset: (OFStreamOffset *)lastHeaderOffset lastHeaderLength: (size_t *)lastHeaderLength { self = [super init]; @try { _archive = [archive retain]; _entry = [entry mutableCopy]; _encoding = encoding; _lastHeaderOffset = lastHeaderOffset; _lastHeaderLength = lastHeaderLength; *_lastHeaderOffset = [stream seekToOffset: 0 whence: OFSeekCurrent]; *_lastHeaderLength = [_entry of_writeToStream: stream encoding: _encoding]; /* * Retain stream last, so that -[close] called by -[dealloc] * doesn't write in case of error. */ _stream = [stream retain]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { if (_stream != nil) [self close]; [_entry release]; if (_archive->_lastReturnedStream == self) _archive->_lastReturnedStream = nil; [_archive release]; [super dealloc]; } - (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; if (UINT32_MAX - _bytesWritten < length) @throw [OFOutOfRangeException exception]; @try { [_stream writeBuffer: buffer length: length]; } @catch (OFWriteFailedException *e) { OFEnsure(e.bytesWritten <= length); _bytesWritten += (uint32_t)e.bytesWritten; _CRC16 = OFCRC16(_CRC16, buffer, e.bytesWritten); if (e.errNo == EWOULDBLOCK || e.errNo == EAGAIN) return e.bytesWritten; @throw e; } _bytesWritten += (uint32_t)length; _CRC16 = OFCRC16(_CRC16, buffer, length); return length; } - (bool)lowlevelIsAtEndOfStream { if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; return _stream.atEndOfStream; } - (int)fileDescriptorForWriting { return ((id <OFReadyForWritingObserving>)_stream) .fileDescriptorForWriting; } - (void)close { OFStreamOffset offset; if (_stream == nil) @throw [OFNotOpenException exceptionWithObject: self]; _entry.uncompressedSize = _bytesWritten; _entry.compressedSize = _bytesWritten; _entry.CRC16 = _CRC16; offset = [_stream seekToOffset: 0 whence: OFSeekCurrent]; if (offset > UINT32_MAX) @throw [OFOutOfRangeException exception]; [_stream seekToOffset: *_lastHeaderOffset whence: OFSeekSet]; _entry->_dataOffset = (uint32_t)offset; OFEnsure([_entry of_writeToStream: _stream encoding: _encoding] == *_lastHeaderLength); [_stream seekToOffset: offset whence: OFSeekSet]; [_stream release]; _stream = nil; [super close]; } @end |
Modified src/OFZooArchiveEntry+Private.h from [4d0560d4d7] to [e4afeb0b7b].
︙ | |||
10 11 12 13 14 15 16 17 18 19 20 21 | 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | + - + + + | * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFZooArchive.h" #import "OFSeekableStream.h" OF_ASSUME_NONNULL_BEGIN @interface OFZooArchiveEntry () - (instancetype)of_init OF_METHOD_FAMILY(init); |
Modified src/OFZooArchiveEntry.m from [ce552a79a7] to [b94a906de8].
︙ | |||
13 14 15 16 17 18 19 20 21 22 | 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | + - + | * file. */ #include "config.h" #import "OFZooArchiveEntry.h" #import "OFZooArchiveEntry+Private.h" #import "OFData.h" #import "OFDate.h" #import "OFNumber.h" #import "OFSeekableStream.h" |
︙ | |||
43 44 45 46 47 48 49 | 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 | - + - + + - + + + + + + - + - - - + - - - + + + + | [self release]; @throw e; } return self; } |
︙ | |||
193 194 195 196 197 198 199 | 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 | - + - - + | { return [self retain]; } - (id)mutableCopy { OFZooArchiveEntry *copy = [[OFMutableZooArchiveEntry alloc] |
︙ | |||
317 318 319 320 321 322 323 324 325 326 327 328 329 330 | 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 | + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + | - (OFNumber *)timeZone { if (_timeZone == 0x7F) return nil; return [OFNumber numberWithFloat: -(float)_timeZone / 4]; } - (size_t)of_writeToStream: (OFSeekableStream *)stream encoding: (OFStringEncoding)encoding { void *pool = objc_autoreleasePoolPush(); OFMutableData *data = [OFMutableData dataWithCapacity: 56]; OFStreamOffset offset = [stream seekToOffset: 0 whence: OFSeekCurrent]; size_t dataOffsetIndex, commentOffsetIndex; char fileNameBuffer[13] = { 0 }; uint8_t tmp8; uint16_t tmp16; uint32_t tmp32; size_t commentLength, fileNameLength, directoryNameLength, length; if (_uncompressedSize > UINT32_MAX || _compressedSize > UINT32_MAX) @throw [OFOutOfRangeException exception]; commentLength = [_fileComment cStringLengthWithEncoding: encoding]; if (commentLength > UINT16_MAX) @throw [OFOutOfRangeException exception]; fileNameLength = [_fileName cStringLengthWithEncoding: encoding]; if (fileNameLength > UINT8_MAX) @throw [OFOutOfRangeException exception]; directoryNameLength = [_directoryName cStringLengthWithEncoding: encoding]; if (directoryNameLength > UINT8_MAX) @throw [OFOutOfRangeException exception]; [data addItems: "\xDC\xA7\xC4\xFD" count: 4]; /* Header type */ [data addItem: "\x02"]; [data addItem: &_compressionMethod]; /* Next header offset filled when writing the next header */ [data increaseCountBy: 4]; /* Data offset is filled after generating the header */ dataOffsetIndex = data.count; [data increaseCountBy: 4]; tmp16 = OFToLittleEndian16(_lastModifiedFileDate); [data addItems: &tmp16 count: 2]; tmp16 = OFToLittleEndian16(_lastModifiedFileTime); [data addItems: &tmp16 count: 2]; tmp16 = OFToLittleEndian16(_CRC16); [data addItems: &tmp16 count: 2]; tmp32 = OFToLittleEndian32((uint32_t)_uncompressedSize); [data addItems: &tmp32 count: 4]; tmp32 = OFToLittleEndian32((uint32_t)_compressedSize); [data addItems: &tmp32 count: 4]; /* Min version needed */ /* TODO: Increase to 2.1 once we add compression. */ [data addItems: "\x02\x00" count: 2]; [data addItem: (_deleted ? "\x01" : "")]; /* * File structure, whatever that is meant to be. * Seems to always be 0. */ [data addItem: ""]; /* Comment offset is filled after generating the header */ commentOffsetIndex = data.count; [data increaseCountBy: 4]; tmp16 = OFToLittleEndian16((uint16_t)commentLength); [data addItems: &tmp16 count: 2]; strncpy(fileNameBuffer, [_fileName cStringWithEncoding: encoding], 12); [data addItems: fileNameBuffer count: 13]; /* Variable length. */ tmp16 = OFToLittleEndian16(fileNameLength + directoryNameLength + 4 + (_POSIXPermissions != nil ? 3 : 0)); [data addItems: &tmp16 count: 2]; [data addItem: &_timeZone]; /* * CRC16 is filled when writing the next header, as the CRC needs to * include the next header offset. */ [data increaseCountBy: 2]; tmp8 = (uint8_t)fileNameLength; [data addItem: &tmp8]; tmp8 = (uint8_t)directoryNameLength; [data addItem: &tmp8]; [data addItems: [_fileName cStringWithEncoding: encoding] count: fileNameLength]; [data addItems: [_directoryName cStringWithEncoding: encoding] count: directoryNameLength]; tmp16 = OFToLittleEndian16((uint16_t)_operatingSystemIdentifier); [data addItems: &tmp16 count: 2]; if (_POSIXPermissions != nil) { unsigned short mode = _POSIXPermissions.unsignedShortValue; uint8_t attributes[3]; attributes[0] = mode & 0xFF; attributes[1] = mode >> 8; attributes[2] = (1 << 6); [data addItems: attributes count: sizeof(attributes)]; } /* Now that we have the entire header, we know where the data starts. */ if (SIZE_MAX - data.count < commentLength) @throw [OFOutOfRangeException exception]; if (offset < 0 || UINT32_MAX - (unsigned long long)offset < data.count + commentLength) @throw [OFOutOfRangeException exception]; tmp32 = OFToLittleEndian32( (uint32_t)offset + (uint32_t)data.count + (uint32_t)commentLength); memcpy([data mutableItemAtIndex: dataOffsetIndex], &tmp32, 4); tmp32 = OFToLittleEndian32((uint32_t)offset + (uint32_t)data.count); memcpy([data mutableItemAtIndex: commentOffsetIndex], &tmp32, 4); [stream writeData: data]; length = data.count; if (commentLength > 0) [stream writeString: _fileComment encoding: encoding]; objc_autoreleasePoolPop(pool); return length; } - (OFString *)description { void *pool = objc_autoreleasePoolPush(); OFString *ret = [OFString stringWithFormat: @"<%@:\n" @"\tFile name = %@\n" |
︙ |