ObjFW  Diff

Differences From Artifact [f436d4f99e]:

To Artifact [379a7cdf89]:


15
16
17
18
19
20
21
22

23
24
25
26
27
28
29
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29







-
+







 */

#include "config.h"

#include <stdio.h>

#import "OFZIPArchive.h"
#import "OFArray.h"
#import "OFZIPArchiveEntry.h"
#import "OFDictionary.h"
#import "OFFile.h"

#import "OFChecksumFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
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
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







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-











-
+







 *  - Encrypted files cannot be read.
 *  - Split archives are not supported.
 *  - Write support is missing.
 *  - The ZIP has to be a file on the local file system.
 *  - No support for ZIP64.
 */

@interface OFZIPArchive_FileHeader: OFObject
{
@public
	uint16_t _madeWithVersion, _minVersion, _generalPurposeBitFlag;
	uint16_t _compressionMethod, _lastModifiedFileTime;
	uint16_t _lastModifiedFileDate;
	uint32_t _CRC32, _compressedSize, _uncompressedSize;
	OFString *_fileName;
	OFDataArray *_extraField;
	OFString *_fileComment;
	uint16_t _startDiskNumber, _internalAttributes;
	uint32_t _externalAttributes, _localFileHeaderOffset;
}

- initWithFile: (OFFile*)file;
@end

@interface OFZIPArchive_LocalFileHeader: OFObject
{
@public
	uint16_t _minVersion, _generalPurposeBitFlag, _compressionMethod;
	uint16_t _lastModifiedFileTime, _lastModifiedFileDate;
	uint32_t _CRC32, _compressedSize, _uncompressedSize;
	OFString *_fileName;
	OFDataArray *_extraField;
}

- initWithFile: (OFFile*)file;
- (bool)matchesFileHeader: (OFZIPArchive_FileHeader*)fileHeader;
- (bool)matchesEntry: (OFZIPArchiveEntry*)entry;
@end

@interface OFZIPArchive_FileStream: OFStream
{
	OFFile *_file;
	size_t _size;
	uint32_t _expectedCRC32, _CRC32;
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
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







-
+













-
-
+








	@try {
		_file = [[OFFile alloc] initWithPath: path
						mode: @"rb"];
		_path = [path copy];

		[self OF_readZIPInfo];
		[self OF_readFileHeaders];
		[self OF_readEntries];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_file release];
	[_path release];
	[_archiveComment release];
	[_filesInArchive release];
	[_fileHeaders release];
	[_entries release];

	[super dealloc];
}

- (void)OF_readZIPInfo
{
	void *pool = objc_autoreleasePoolPush();
166
167
168
169
170
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
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
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
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
212
213
214
215
216







-
+







-
-
+


-
-
-
+
+

+
+
-
-
-
+
+
+


-
-
+




-
+

-
+











-
+


-
+





-
+




-
+







	_archiveComment = [[_file
	    readStringWithLength: commentLength
			encoding: OF_STRING_ENCODING_CODEPAGE_437] copy];

	objc_autoreleasePoolPop(pool);
}

- (void)OF_readFileHeaders
- (void)OF_readEntries
{
	void *pool = objc_autoreleasePoolPush();
	size_t i;

	[_file seekToOffset: _centralDirectoryOffset
		     whence: SEEK_SET];

	_filesInArchive = [[OFMutableArray alloc] init];
	_fileHeaders = [[OFMutableDictionary alloc] init];
	_entries = [[OFMutableDictionary alloc] init];

	for (i = 0; i < _centralDirectoryEntries; i++) {
		OFZIPArchive_FileHeader *fileHeader =
		    [[[OFZIPArchive_FileHeader alloc]
		    initWithFile: _file] autorelease];
		OFZIPArchiveEntry *entry = [[[OFZIPArchiveEntry alloc]
		    OF_initWithFile: _file] autorelease];

		if ([_entries objectForKey: [entry fileName]] != nil)
			@throw [OFInvalidFormatException exception];
		[_filesInArchive addObject: fileHeader->_fileName];
		[_fileHeaders setObject: fileHeader
				 forKey: fileHeader->_fileName];

		[_entries setObject: entry
			     forKey: [entry fileName]];
	}

	[_filesInArchive makeImmutable];
	[_fileHeaders makeImmutable];
	[_entries makeImmutable];

	objc_autoreleasePoolPop(pool);
}

- (OFArray*)filesInArchive
- (OFDictionary*)entries
{
	OF_GETTER(_filesInArchive, true)
	OF_GETTER(_entries, true)
}

- (OFString*)archiveComment
{
	OF_GETTER(_archiveComment, true)
}

- (OFStream*)streamForReadingFile: (OFString*)path
{
	OFStream *ret;
	void *pool = objc_autoreleasePoolPush();
	OFZIPArchive_FileHeader *fileHeader = [_fileHeaders objectForKey: path];
	OFZIPArchiveEntry *entry = [_entries objectForKey: path];
	OFZIPArchive_LocalFileHeader *localFileHeader;

	if (fileHeader == nil) {
	if (entry == nil) {
		errno = ENOENT;
		@throw [OFOpenFileFailedException exceptionWithPath: path
							       mode: @"rb"];
	}

	[_file seekToOffset: fileHeader->_localFileHeaderOffset
	[_file seekToOffset: [entry OF_localFileHeaderOffset]
		     whence: SEEK_SET];
	localFileHeader = [[[OFZIPArchive_LocalFileHeader alloc]
	    initWithFile: _file] autorelease];

	if (![localFileHeader matchesFileHeader: fileHeader])
	if (![localFileHeader matchesEntry: entry])
		@throw [OFInvalidFormatException exception];

	if (localFileHeader->_minVersion > 10) {
		OFString *version = [OFString stringWithFormat: @"%u.%u",
		    localFileHeader->_minVersion / 10,
		    localFileHeader->_minVersion % 10];

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
229
230
231
232
233
234
235




























































236
237
238
239
240
241
242







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







			   size: localFileHeader->_uncompressedSize
			  CRC32: localFileHeader->_CRC32];

	objc_autoreleasePoolPop(pool);

	return [ret autorelease];
}
@end

@implementation OFZIPArchive_FileHeader
- initWithFile: (OFFile*)file
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		uint16_t fileNameLength, extraFieldLength, fileCommentLength;
		of_string_encoding_t encoding;

		if ([file readLittleEndianInt32] != 0x02014B50)
			@throw [OFInvalidFormatException exception];

		_madeWithVersion = [file readLittleEndianInt16];
		_minVersion = [file readLittleEndianInt16];
		_generalPurposeBitFlag = [file readLittleEndianInt16];
		_compressionMethod = [file readLittleEndianInt16];
		_lastModifiedFileTime = [file readLittleEndianInt16];
		_lastModifiedFileDate = [file readLittleEndianInt16];
		_CRC32 = [file readLittleEndianInt32];
		_compressedSize = [file readLittleEndianInt32];
		_uncompressedSize = [file readLittleEndianInt32];
		fileNameLength = [file readLittleEndianInt16];
		extraFieldLength = [file readLittleEndianInt16];
		fileCommentLength = [file readLittleEndianInt16];
		_startDiskNumber = [file readLittleEndianInt16];
		_internalAttributes = [file readLittleEndianInt16];
		_externalAttributes = [file readLittleEndianInt32];
		_localFileHeaderOffset = [file readLittleEndianInt32];

		encoding = (_generalPurposeBitFlag & (1 << 11)
		    ? OF_STRING_ENCODING_UTF_8
		    : OF_STRING_ENCODING_CODEPAGE_437);

		_fileName = [[file readStringWithLength: fileNameLength
					       encoding: encoding] copy];
		_extraField = [[file
		    readDataArrayWithCount: extraFieldLength] retain];
		_fileComment = [[file readStringWithLength: fileCommentLength
						  encoding: encoding] copy];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_fileName release];
	[_extraField release];
	[_fileComment release];

	[super dealloc];
}
@end

@implementation OFZIPArchive_LocalFileHeader
- initWithFile: (OFFile*)file
{
	self = [super init];

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







-
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+







{
	[_fileName release];
	[_extraField release];

	[super dealloc];
}

- (bool)matchesFileHeader: (OFZIPArchive_FileHeader*)fileHeader
- (bool)matchesEntry: (OFZIPArchiveEntry*)entry
{
	if (_minVersion != fileHeader->_minVersion ||
	    _generalPurposeBitFlag != fileHeader->_generalPurposeBitFlag ||
	    _compressionMethod != fileHeader->_compressionMethod ||
	    _lastModifiedFileTime != fileHeader->_lastModifiedFileTime ||
	    _lastModifiedFileDate != fileHeader->_lastModifiedFileDate ||
	    _CRC32 != fileHeader->_CRC32 ||
	    _compressedSize != fileHeader->_compressedSize ||
	    _uncompressedSize != fileHeader->_uncompressedSize ||
	    ![_fileName isEqual: fileHeader->_fileName])
	if (_minVersion != [entry OF_minVersion] ||
	    _generalPurposeBitFlag != [entry OF_generalPurposeBitFlag] ||
	    _compressionMethod != [entry OF_compressionMethod] ||
	    _lastModifiedFileTime != [entry OF_lastModifiedFileTime] ||
	    _lastModifiedFileDate != [entry OF_lastModifiedFileDate] ||
	    _CRC32 != [entry CRC32] ||
	    _compressedSize != [entry compressedSize] ||
	    _uncompressedSize != [entry uncompressedSize] ||
	    ![_fileName isEqual: [entry fileName]])
		return false;

	return true;
}

@end