ObjFW  Diff

Differences From Artifact [c62bbdf049]:

To Artifact [4c67ad00cf]:


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
174
175
176
177
178

179
180
181
182
183
184
185
186
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

174
175
176
177
178
179
180







-
+
-











-
+
-

-
+
-



-
+
-

-
+
-








-
+
-







}

static void
seekOrThrowInvalidFormat(OFSeekableStream *stream,
    of_offset_t offset, int whence)
{
	@try {
		[stream seekToOffset: offset
		[stream seekToOffset: offset whence: whence];
			      whence: whence];
	} @catch (OFSeekFailedException *e) {
		if (e.errNo == EINVAL)
			@throw [OFInvalidFormatException exception];

		@throw e;
	}
}

@implementation OFZIPArchive
@synthesize archiveComment = _archiveComment;

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

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

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

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

	@try {
		if ([mode isEqual: @"r"])
			_mode = OF_ZIP_ARCHIVE_MODE_READ;
		else if ([mode isEqual: @"w"])
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
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







-
+
-




-
+
-

-
+
-


-
+
-







		@throw e;
	}

	return self;
}

#ifdef OF_HAVE_FILES
- (instancetype)initWithPath: (OFString *)path
- (instancetype)initWithPath: (OFString *)path mode: (OFString *)mode
			mode: (OFString *)mode
{
	OFFile *file;

	if ([mode isEqual: @"a"])
		file = [[OFFile alloc] initWithPath: path
		file = [[OFFile alloc] initWithPath: path mode: @"r+"];
					       mode: @"r+"];
	else
		file = [[OFFile alloc] initWithPath: path
		file = [[OFFile alloc] initWithPath: path mode: mode];
					       mode: mode];

	@try {
		self = [self initWithStream: file
		self = [self initWithStream: file mode: mode];
				       mode: mode];
	} @finally {
		[file release];
	}

	return self;
}
#endif
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
778
779
780
781
782
783
784

785

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







-
+
-




















-
+
-







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

	return _atEndOfStream;
}

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

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

	if (_atEndOfStream)
		return 0;

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

#if SIZE_MAX >= UINT64_MAX
	if (length > UINT64_MAX)
		@throw [OFOutOfRangeException exception];
#endif

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

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

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

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

884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
899
900
901
902
903
904

905
906
907
908
909
910
911
912
872
873
874
875
876
877
878

879

880
881
882
883
884
885
886
887
888
889
890

891

892
893
894
895
896
897
898







-
+
-











-
+
-







		[self close];

	[_entry release];

	[super dealloc];
}

- (size_t)lowlevelWriteBuffer: (const void *)buffer
- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length
		       length: (size_t)length
{
	size_t bytesWritten;

#if SIZE_MAX >= INT64_MAX
	if (length > INT64_MAX)
		@throw [OFOutOfRangeException exception];
#endif

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

	bytesWritten = [_stream writeBuffer: buffer
	bytesWritten = [_stream writeBuffer: buffer length: length];
				     length: length];

	_bytesWritten += (int64_t)bytesWritten;
	_CRC32 = of_crc32(_CRC32, buffer, length);

	return bytesWritten;
}