ObjFW  Diff

Differences From Artifact [56c946d958]:

To Artifact [f05c35edb7]:


39
40
41
42
43
44
45

46
47
48
49
50
51
52
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFSeekFailedException.h"

#import "OFUnsupportedVersionException.h"

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







>







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFOpenItemFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFSeekFailedException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedVersionException.h"

/*
 * FIXME: Current limitations:
 *  - Split archives are not supported.
 *  - Encrypted files cannot be read.
 */
71
72
73
74
75
76
77

78
79
80
81
82
83
84
85

- (instancetype)initWithStream: (OFStream *)stream;
- (bool)matchesEntry: (OFZIPArchiveEntry *)entry;
@end

@interface OFZIPArchive_FileReadStream: OFStream
{

	OFStream *_stream, *_decompressedStream;
	OFZIPArchiveEntry *_entry;
	uint64_t _toRead;
	uint32_t _CRC32;
	bool _atEndOfStream;
}

- (instancetype)of_initWithStream: (OFStream *)stream







>
|







72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

- (instancetype)initWithStream: (OFStream *)stream;
- (bool)matchesEntry: (OFZIPArchiveEntry *)entry;
@end

@interface OFZIPArchive_FileReadStream: OFStream
{
	OF_KINDOF(OFStream *) _stream;
	OF_KINDOF(OFStream *) _decompressedStream;
	OFZIPArchiveEntry *_entry;
	uint64_t _toRead;
	uint32_t _CRC32;
	bool _atEndOfStream;
}

- (instancetype)of_initWithStream: (OFStream *)stream
774
775
776
777
778
779
780



781
782
783
784
785
786
787
	[_entry release];

	[super dealloc];
}

- (bool)lowlevelIsAtEndOfStream
{



	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	size_t ret;







>
>
>







776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
	[_entry release];

	[super dealloc];
}

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

	return _atEndOfStream;
}

- (size_t)lowlevelReadIntoBuffer: (void *)buffer
			  length: (size_t)length
{
	size_t ret;
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

	if ((uint64_t)length > _toRead)
		length = (size_t)_toRead;

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

	if (ret == 0)
		_atEndOfStream = true;

	_toRead -= ret;
	_CRC32 = of_crc32(_CRC32, buffer, ret);

	if (_toRead == 0)


		if (~_CRC32 != [_entry CRC32])
			@throw [OFChecksumFailedException exception];


	return ret;
}












- (void)close
{
	[_stream release];
	_stream = nil;




	[super close];
}
@end

@implementation OFZIPArchive_FileWriteStream
- (instancetype)initWithStream: (OFStream *)stream







<
<
<



|
>
>


>



>
>
>
>
>
>
>
>
>
>
>





>
>
>







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

	if ((uint64_t)length > _toRead)
		length = (size_t)_toRead;

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




	_toRead -= ret;
	_CRC32 = of_crc32(_CRC32, buffer, ret);

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

		if (~_CRC32 != [_entry CRC32])
			@throw [OFChecksumFailedException exception];
	}

	return ret;
}

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

- (int)fileDescriptorForReading
{
	return [_decompressedStream fileDescriptorForReading];
}

- (void)close
{
	[_stream release];
	_stream = nil;

	[_decompressedStream release];
	_decompressedStream = nil;

	[super close];
}
@end

@implementation OFZIPArchive_FileWriteStream
- (instancetype)initWithStream: (OFStream *)stream