ObjFW  Diff

Differences From Artifact [2bb1e08022]:

To Artifact [952e87c05c]:


44
45
46
47
48
49
50






51
52
53
54
55
56
57
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63







+
+
+
+
+
+







#import "OFUnsupportedVersionException.h"

/*
 * FIXME: Current limitations:
 *  - Split archives are not supported.
 *  - Encrypted files cannot be read.
 */

enum {
	modeRead,
	modeWrite,
	modeAppend
};

OF_DIRECT_MEMBERS
@interface OFZIPArchive ()
- (void)of_readZIPInfo;
- (void)of_readEntries;
- (void)of_closeLastReturnedStream;
- (void)of_writeCentralDirectory;
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
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







-
+

-
+

-
+







-
+
-







-
+








- (instancetype)initWithStream: (OFStream *)stream mode: (OFString *)mode
{
	self = [super init];

	@try {
		if ([mode isEqual: @"r"])
			_mode = OFZIPArchiveModeRead;
			_mode = modeRead;
		else if ([mode isEqual: @"w"])
			_mode = OFZIPArchiveModeWrite;
			_mode = modeWrite;
		else if ([mode isEqual: @"a"])
			_mode = OFZIPArchiveModeAppend;
			_mode = modeAppend;
		else
			@throw [OFInvalidArgumentException exception];

		_stream = [stream retain];
		_entries = [[OFMutableArray alloc] init];
		_pathToEntryMap = [[OFMutableDictionary alloc] init];

		if (_mode == OFZIPArchiveModeRead ||
		if (_mode == modeRead || _mode == modeAppend) {
		    _mode == OFZIPArchiveModeAppend) {
			if (![stream isKindOfClass: [OFSeekableStream class]])
				@throw [OFInvalidArgumentException exception];

			[self of_readZIPInfo];
			[self of_readEntries];
		}

		if (_mode == OFZIPArchiveModeAppend) {
		if (_mode == modeAppend) {
			_offset = _centralDirectoryOffset;
			seekOrThrowInvalidFormat((OFSeekableStream *)_stream,
			    (OFFileOffset)_offset, SEEK_SET);
		}
	} @catch (id e) {
		/*
		 * If we are in write or append mode, we do not want -[close]
396
397
398
399
400
401
402
403

404
405
406
407
408
409
410
411
401
402
403
404
405
406
407

408

409
410
411
412
413
414
415







-
+
-







{
	@try {
		[_lastReturnedStream close];
	} @catch (OFNotOpenException *e) {
		/* Might have already been closed by the user - that's fine. */
	}

	if ((_mode == OFZIPArchiveModeWrite ||
	if ((_mode == modeWrite || _mode == modeAppend) &&
	    _mode == OFZIPArchiveModeAppend) &&
	    [_lastReturnedStream isKindOfClass:
	    [OFZIPArchiveFileWriteStream class]]) {
		OFZIPArchiveFileWriteStream *stream =
		    (OFZIPArchiveFileWriteStream *)_lastReturnedStream;

		if (INT64_MAX - _offset < stream->_bytesWritten)
			@throw [OFOutOfRangeException exception];
426
427
428
429
430
431
432
433

434
435
436
437
438
439
440
430
431
432
433
434
435
436

437
438
439
440
441
442
443
444







-
+







- (OFStream *)streamForReadingFile: (OFString *)path
{
	void *pool = objc_autoreleasePoolPush();
	OFZIPArchiveEntry *entry;
	OFZIPArchiveLocalFileHeader *localFileHeader;
	int64_t offset64;

	if (_mode != OFZIPArchiveModeRead)
	if (_mode != modeRead)
		@throw [OFInvalidArgumentException exception];

	if ((entry = [_pathToEntryMap objectForKey: path]) == nil)
		@throw [OFOpenItemFailedException exceptionWithPath: path
							       mode: @"r"
							      errNo: ENOENT];

476
477
478
479
480
481
482
483

484
485
486
487
488
489
490
480
481
482
483
484
485
486

487
488
489
490
491
492
493
494







-
+







	int64_t offsetAdd = 0;
	void *pool;
	OFMutableZIPArchiveEntry *entry;
	OFString *fileName;
	OFData *extraField;
	uint16_t fileNameLength, extraFieldLength;

	if (_mode != OFZIPArchiveModeWrite && _mode != OFZIPArchiveModeAppend)
	if (_mode != modeWrite && _mode != modeAppend)
		@throw [OFInvalidArgumentException exception];

	pool = objc_autoreleasePoolPush();
	entry = [[entry_ mutableCopy] autorelease];

	if ([_pathToEntryMap objectForKey: entry.fileName] != nil)
		@throw [OFOpenItemFailedException
609
610
611
612
613
614
615
616

617
618
619
620
621
622
623
613
614
615
616
617
618
619

620
621
622
623
624
625
626
627







-
+







- (void)close
{
	if (_stream == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

	[self of_closeLastReturnedStream];

	if (_mode == OFZIPArchiveModeWrite || _mode == OFZIPArchiveModeAppend)
	if (_mode == modeWrite || _mode == modeAppend)
		[self of_writeCentralDirectory];

	[_stream release];
	_stream = nil;
}
@end