ObjFW  Diff

Differences From Artifact [9fc92038be]:

To Artifact [12097984b6]:


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
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







-
-
+
+






-
+








-
+





-
+







-
+




















-
+







#import "OFNotOpenException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFWriteFailedException.h"

@interface OFLHAArchive_FileReadStream: OFStream <OFReadyForReadingObserving>
{
	OF_KINDOF(OFStream *) _stream;
	OF_KINDOF(OFStream *) _decompressedStream;
	OFStream *_stream;
	OFStream *_decompressedStream;
	OFLHAArchiveEntry *_entry;
	uint32_t _toRead, _bytesConsumed;
	uint16_t _CRC16;
	bool _atEndOfStream, _skipped;
}

- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
- (instancetype)of_initWithStream: (OFStream *)stream
			    entry: (OFLHAArchiveEntry *)entry;
- (void)of_skip;
@end

@interface OFLHAArchive_FileWriteStream: OFStream <OFReadyForWritingObserving>
{
	OFMutableLHAArchiveEntry *_entry;
	of_string_encoding_t _encoding;
	OF_KINDOF(OFStream *) _stream;
	OFSeekableStream *_stream;
	of_offset_t _headerOffset;
	uint32_t _bytesWritten;
	uint16_t _CRC16;
}

- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
- (instancetype)of_initWithStream: (OFSeekableStream *)stream
			    entry: (OFLHAArchiveEntry *)entry
			 encoding: (of_string_encoding_t)encoding;
@end

@implementation OFLHAArchive
@synthesize encoding = _encoding;

+ (instancetype)archiveWithStream: (OF_KINDOF(OFStream *))stream
+ (instancetype)archiveWithStream: (OFStream *)stream
			     mode: (OFString *)mode
{
	return [[[self alloc] initWithStream: stream
					mode: mode] autorelease];
}

#ifdef OF_HAVE_FILES
+ (instancetype)archiveWithPath: (OFString *)path
			   mode: (OFString *)mode
{
	return [[[self alloc] initWithPath: path
				      mode: mode] autorelease];
}
#endif

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

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

	@try {
		_stream = [stream retain];

111
112
113
114
115
116
117
118
119


120
121
122
123
124
125
126
111
112
113
114
115
116
117


118
119
120
121
122
123
124
125
126







-
-
+
+








		if ((_mode == OF_LHA_ARCHIVE_MODE_WRITE ||
		    _mode == OF_LHA_ARCHIVE_MODE_APPEND) &&
		    ![_stream isKindOfClass: [OFSeekableStream class]])
			@throw [OFInvalidArgumentException exception];

		if (_mode == OF_LHA_ARCHIVE_MODE_APPEND)
			[_stream seekToOffset: 0
				       whence: SEEK_END];
			[(OFSeekableStream *)_stream seekToOffset: 0
							   whence: SEEK_END];

		_encoding = OF_STRING_ENCODING_ISO_8859_1;
	} @catch (id e) {
		[self release];
		@throw e;
	}

163
164
165
166
167
168
169
170

171
172
173
174
175
176

177
178
179
180
181
182
183
163
164
165
166
167
168
169

170
171
172
173
174
175

176
177
178
179
180
181
182
183







-
+





-
+







	OFLHAArchiveEntry *entry;
	char header[21];
	size_t headerLen;

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

	[_lastReturnedStream of_skip];
	[(OFLHAArchive_FileReadStream *)_lastReturnedStream of_skip];
	[_lastReturnedStream close];
	[_lastReturnedStream release];
	_lastReturnedStream = nil;

	for (headerLen = 0; headerLen < 21;) {
		if ([_stream isAtEndOfStream]) {
		if (_stream.atEndOfStream) {
			if (headerLen == 0)
				return nil;

			if (headerLen == 1 && header[0] == 0)
				return nil;

			@throw [OFTruncatedDataException exception];
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
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







+
-
+











-
+











-
+



+
-
+

















-
+









-
+







{
	if (_mode != OF_LHA_ARCHIVE_MODE_READ)
		@throw [OFInvalidArgumentException exception];

	if (_lastReturnedStream == nil)
		@throw [OFInvalidArgumentException exception];

	return [[(OFLHAArchive_FileReadStream *)_lastReturnedStream
	return [[_lastReturnedStream retain] autorelease];
	    retain] autorelease];
}

- (OFStream <OFReadyForWritingObserving> *)
    streamForWritingEntry: (OFLHAArchiveEntry *)entry
{
	OFString *compressionMethod;

	if (_mode != OF_LHA_ARCHIVE_MODE_WRITE &&
	    _mode != OF_LHA_ARCHIVE_MODE_APPEND)
		@throw [OFInvalidArgumentException exception];

	compressionMethod = [entry compressionMethod];
	compressionMethod = entry.compressionMethod;

	if (![compressionMethod isEqual: @"-lh0-"] &&
	    ![compressionMethod isEqual: @"-lhd-"])
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	[_lastReturnedStream close];
	[_lastReturnedStream release];
	_lastReturnedStream = nil;

	_lastReturnedStream = [[OFLHAArchive_FileWriteStream alloc]
	    of_initWithStream: _stream
	    of_initWithStream: (OFSeekableStream *)_stream
			entry: entry
		     encoding: _encoding];

	return [[(OFLHAArchive_FileWriteStream *)_lastReturnedStream
	return [[_lastReturnedStream retain] autorelease];
	    retain] autorelease];
}

- (void)close
{
	if (_stream == nil)
		return;

	[_lastReturnedStream close];
	[_lastReturnedStream release];
	_lastReturnedStream = nil;

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

@implementation OFLHAArchive_FileReadStream
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
- (instancetype)of_initWithStream: (OFStream *)stream
			    entry: (OFLHAArchiveEntry *)entry
{
	self = [super init];

	@try {
		OFString *compressionMethod;

		_stream = [stream retain];

		compressionMethod = [entry compressionMethod];
		compressionMethod = entry.compressionMethod;

		if ([compressionMethod isEqual: @"-lh4-"] ||
		    [compressionMethod isEqual: @"-lh5-"])
			_decompressedStream = [[OFLHAArchive_LHStream alloc]
			    of_initWithStream: stream
				 distanceBits: 4
			       dictionaryBits: 14];
281
282
283
284
285
286
287
288

289
290
291
292
293
294
295
283
284
285
286
287
288
289

290
291
292
293
294
295
296
297







-
+







			    of_initWithStream: stream
				 distanceBits: 5
			       dictionaryBits: 17];
		else
			_decompressedStream = [stream retain];

		_entry = [entry copy];
		_toRead = [entry uncompressedSize];
		_toRead = entry.uncompressedSize;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
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
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







-
-
+














-
+



-
+












-
-
+
+




+
-
+




-
+














-
+
+


-
+






-
-
+
+








	if (_stream == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (_atEndOfStream)
		return 0;

	if ([_stream isAtEndOfStream] &&
	    ![_decompressedStream hasDataInReadBuffer])
	if (_stream.atEndOfStream && !_decompressedStream.hasDataInReadBuffer)
		@throw [OFTruncatedDataException exception];

	if (length > _toRead)
		length = _toRead;

	ret = [_decompressedStream readIntoBuffer: buffer
					   length: length];

	_toRead -= ret;
	_CRC16 = of_crc16(_CRC16, buffer, ret);

	if (_toRead == 0) {
		_atEndOfStream = true;

		if (_CRC16 != [_entry CRC16]) {
		if (_CRC16 != _entry.CRC16) {
			OFString *actualChecksum = [OFString stringWithFormat:
			    @"%04" PRIX16, _CRC16];
			OFString *expectedChecksum = [OFString stringWithFormat:
			    @"%04" PRIX16, [_entry CRC16]];
			    @"%04" PRIX16, _entry.CRC16];

			@throw [OFChecksumMismatchException
			    exceptionWithActualChecksum: actualChecksum
				       expectedChecksum: expectedChecksum];
		}
	}

	return ret;
}

- (bool)hasDataInReadBuffer
{
	return ([super hasDataInReadBuffer] ||
	    [_decompressedStream hasDataInReadBuffer]);
	return (super.hasDataInReadBuffer ||
	    _decompressedStream.hasDataInReadBuffer);
}

- (int)fileDescriptorForReading
{
	return ((id <OFReadyForReadingObserving>)_decompressedStream)
	return [_decompressedStream fileDescriptorForReading];
	    .fileDescriptorForReading;
}

- (void)of_skip
{
	OF_KINDOF(OFStream *) stream;
	OFStream *stream;
	uint32_t toRead;

	if (_stream == nil || _skipped)
		return;

	stream = _stream;
	toRead = _toRead;

	/*
	 * Get the number of consumed bytes and directly read from the
	 * compressed stream, to make skipping much faster.
	 */
	if ([_decompressedStream isKindOfClass:
	    [OFLHAArchive_LHStream class]]) {
		OFLHAArchive_LHStream *LHStream = _decompressedStream;
		OFLHAArchive_LHStream *LHStream =
		    (OFLHAArchive_LHStream *)_decompressedStream;

		[LHStream close];
		toRead = [_entry compressedSize] - LHStream->_bytesConsumed;
		toRead = _entry.compressedSize - LHStream->_bytesConsumed;

		stream = _stream;
	}

	if ([stream isKindOfClass: [OFSeekableStream class]] &&
	    (sizeof(of_offset_t) > 4 || toRead < INT32_MAX))
		[stream seekToOffset: (of_offset_t)toRead
			      whence: SEEK_CUR];
		[(OFSeekableStream *)stream seekToOffset: (of_offset_t)toRead
						  whence: SEEK_CUR];
	else {
		while (toRead > 0) {
			char buffer[512];
			size_t min = toRead;

			if (min > 512)
				min = 512;
423
424
425
426
427
428
429
430

431
432
433
434
435
436
437
426
427
428
429
430
431
432

433
434
435
436
437
438
439
440







-
+







	_decompressedStream = nil;

	[super close];
}
@end

@implementation OFLHAArchive_FileWriteStream
- (instancetype)of_initWithStream: (OF_KINDOF(OFStream *))stream
- (instancetype)of_initWithStream: (OFSeekableStream *)stream
			    entry: (OFLHAArchiveEntry *)entry
			 encoding: (of_string_encoding_t)encoding
{
	self = [super init];

	@try {
		_entry = [entry mutableCopy];
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
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







-
-
+
+















-
+




+
-
+









-
-
-
+
+
+







	if (UINT32_MAX - _bytesWritten < length)
		@throw [OFOutOfRangeException exception];

	@try {
		bytesWritten = (uint32_t)[_stream writeBuffer: buffer
						       length: length];
	} @catch (OFWriteFailedException *e) {
		_bytesWritten += [e bytesWritten];
		_CRC16 = of_crc16(_CRC16, buffer, [e bytesWritten]);
		_bytesWritten += e.bytesWritten;
		_CRC16 = of_crc16(_CRC16, buffer, e.bytesWritten);

		@throw e;
	}

	_bytesWritten += (uint32_t)bytesWritten;
	_CRC16 = of_crc16(_CRC16, buffer, bytesWritten);

	return bytesWritten;
}

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

	return [_stream isAtEndOfStream];
	return _stream.atEndOfStream;
}

- (int)fileDescriptorForWriting
{
	return ((id <OFReadyForWritingObserving>)_stream)
	return [_stream fileDescriptorForWriting];
	    .fileDescriptorForWriting;
}

- (void)close
{
	of_offset_t offset;

	if (_stream == nil)
		return;

	[_entry setUncompressedSize: _bytesWritten];
	[_entry setCompressedSize: _bytesWritten];
	[_entry setCRC16: _CRC16];
	_entry.uncompressedSize = _bytesWritten;
	_entry.compressedSize = _bytesWritten;
	_entry.CRC16 = _CRC16;

	offset = [_stream seekToOffset: 0
				whence:SEEK_CUR];
	[_stream seekToOffset: _headerOffset
		       whence: SEEK_SET];
	[_entry of_writeToStream: _stream
			encoding: _encoding];