ObjFW  Check-in [57f9bc6ace]

Overview
Comment:OFLHAArchiveEntry: Add support for LHA level 0
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 57f9bc6ace41c1951e8fb94a6c7740a417c3f974c333468242f6ff24b9eeb981
User & Date: js on 2018-05-27 11:07:03
Other Links: manifest | tags
Context
2018-05-27
11:14
ofzip: Only print OS identifier if it exists check-in: bfc99fa4ac user: js tags: trunk
11:07
OFLHAArchiveEntry: Add support for LHA level 0 check-in: 57f9bc6ace user: js tags: trunk
09:44
ofzip: Add support for LHA archives check-in: c693041e37 user: js tags: trunk
Changes

Modified src/OFLHAArchive.h from [874facd305] to [94d0a087a6].

29
30
31
32
33
34
35

36
37
38
39
40
41
42
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43







+







 *
 * @brief A class for accessing and manipulating LHA files.
 */
@interface OFLHAArchive: OFObject
{
	OF_KINDOF(OFStream *) _stream;
	of_string_encoding_t _encoding;
	OFLHAArchiveEntry *_Nullable _lastEntry;
	OF_KINDOF(OFStream *) _Nullable _lastReturnedStream;
}

/*!
 * @brief The encoding to use for the archive. Defaults to ISO 8859-1.
 */
@property (nonatomic) of_string_encoding_t encoding;

Modified src/OFLHAArchive.m from [dc06a91f23] to [1011e54787].

26
27
28
29
30
31
32

33
34
35
36
37
38
39
40
41
42
43
26
27
28
29
30
31
32
33
34
35
36

37
38
39
40
41
42
43







+



-







#import "OFStream.h"
#import "OFSeekableStream.h"
#import "OFString.h"

#import "OFInvalidArgumentException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFTruncatedDataException.h"

@interface OFLHAArchive_FileReadStream: OFStream <OFReadyForReadingObserving>
{
	OFLHAArchiveEntry *_entry;
	OF_KINDOF(OFStream *) _stream;
	uint32_t _toRead;
	bool _atEndOfStream;
}

- (instancetype)of_initWithStream: (OFStream *)stream
			    entry: (OFLHAArchiveEntry *)entry;
118
119
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
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
118
119
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
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
217
218
219
220
221
222
223


224
225
226
227
228
229
230







-
-
+
+
+
+
+






+
-
-
+
+
+

-
-
-
+
+

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



-
+

-
+




+
+


+
+
+
+
+
+
+
+








+
+
+

















-
-
-
-
-
-
-
-

+
+
+
+
+
-
+












-
-







	[self close];

	[super dealloc];
}

- (OFLHAArchiveEntry *)nextEntry
{
	OFLHAArchiveEntry *entry;
	uint8_t headerSize;
	char header[21];
	size_t headerLen;

	[_lastEntry release];
	_lastEntry = nil;

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

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

	if ([_stream readIntoBuffer: &headerSize
			     length: 1] == 0)
		return nil;
			if (headerLen == 1 && header[0] == 0)
				return nil;

			@throw [OFTruncatedDataException exception];
		}
	if (headerSize == 0)

		return nil;

	entry = [[[OFLHAArchiveEntry alloc]
	    of_initWithHeaderSize: headerSize
			   stream: _stream
			 encoding: _encoding] autorelease];
		headerLen += [_stream readIntoBuffer: header + headerLen
					      length: 21 - headerLen];
	}

	_lastEntry = [[OFLHAArchiveEntry alloc]
	    of_initWithHeader: header
		       stream: _stream
		     encoding: _encoding];

	_lastReturnedStream = [[OFLHAArchive_FileReadStream alloc]
	    of_initWithStream: _stream
			entry: entry];
			entry: _lastEntry];

	return entry;
	return [[_lastEntry copy] autorelease];
}

- (OFStream <OFReadyForReadingObserving> *)streamForReadingCurrentEntry
{
	OFString *method;

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

	method = [_lastEntry method];

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

	return [[_lastReturnedStream retain] autorelease];
}

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

	[_lastEntry release];
	_lastEntry = nil;

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

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

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

	@try {
		OFString *method = [entry method];

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

		_entry = [entry copy];
		_stream = [stream retain];
		/*
		 * Use the compressed size, as that is the number of bytes we
		 * need to skip for the next entry and is equal for
		 * uncompressed files, the only thing supported so far.
		 */
		_toRead = [entry uncompressedSize];
		_toRead = [entry compressedSize];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[self close];

	[_entry release];

	[super dealloc];
}

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

Modified src/OFLHAArchiveEntry+Private.h from [2c161f93e6] to [d79a363508].

16
17
18
19
20
21
22
23
24
25



26
27
28
29
16
17
18
19
20
21
22



23
24
25
26
27
28
29







-
-
-
+
+
+




 */

#import "OFLHAArchive.h"

OF_ASSUME_NONNULL_BEGIN

@interface OFLHAArchiveEntry ()
- (instancetype)of_initWithHeaderSize: (uint8_t)headerSize
			       stream: (OFStream *)stream
			     encoding: (of_string_encoding_t)encoding
- (instancetype)of_initWithHeader: (char [_Nonnull 21])header
			   stream: (OFStream *)stream
			 encoding: (of_string_encoding_t)encoding
    OF_METHOD_FAMILY(init);
@end

OF_ASSUME_NONNULL_END

Modified src/OFLHAArchiveEntry.h from [7d4437acfc] to [88951356da].

18
19
20
21
22
23
24

25
26
27
28
29
30
31
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32







+







#import "OFObject.h"

OF_ASSUME_NONNULL_BEGIN

@class OFArray OF_GENERIC(ObjectType);
@class OFData;
@class OFDate;
@class OFMutableArray OF_GENERIC(ObjectType);
@class OFNumber;
@class OFString;

/*!
 * @class OFLHAArchiveEntry OFLHAArchiveEntry.h ObjFW/OFLHAArchiveEntry.h
 *
 * @brief A class which represents an entry in the central directory of a LHA
42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57







-
+







	uint8_t _level;
	uint16_t _CRC16;
	uint8_t _operatingSystemIdentifier;
	OFString *_Nullable _fileComment;
	OFNumber *_Nullable _mode, *_Nullable _UID, *_Nullable _GID;
	OFString *_Nullable _owner, *_Nullable _group;
	OFDate *_Nullable _modificationDate;
	OFArray OF_GENERIC(OFData *) *_extensions;
	OFMutableArray OF_GENERIC(OFData *) *_extensions;
}

/*!
 * @brief The file name of the entry.
 */
@property (readonly, copy, nonatomic) OFString *fileName;

Modified src/OFLHAArchiveEntry.m from [596d16c180] to [5976df7f2a].

26
27
28
29
30
31
32



















33
34
35
36
37
38
39
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#import "OFDate.h"
#import "OFNumber.h"
#import "OFStream.h"
#import "OFString.h"

#import "OFInvalidFormatException.h"
#import "OFUnsupportedVersionException.h"

static OFDate *
parseMSDOSDate(uint32_t MSDOSDate)
{
	uint16_t year = ((MSDOSDate & 0xFE000000) >> 25) + 1980;
	uint8_t month = (MSDOSDate & 0x1E00000) >> 21;
	uint8_t day = (MSDOSDate & 0x1F);
	uint8_t hour = (MSDOSDate & 0xF800) >> 11;
	uint8_t minute = (MSDOSDate & 0x7E0) >> 5;
	uint8_t second = (MSDOSDate & 0x1F) << 1;
	OFString *dateString;

	dateString = [OFString
	    stringWithFormat: @"%04u-%02u-%02u %02u:%02u:%02u",
			      year, month, day, hour, minute, second];

	return [OFDate dateWithLocalDateString: dateString
					format: @"%Y-%m-%d %H:%M:%S"];
}

static void
parseFileNameExtension(OFLHAArchiveEntry *entry, OFData *extension,
    of_string_encoding_t encoding)
{
	[entry->_fileName release];
	entry->_fileName = nil;
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
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
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
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
322
323
324
325
326
327
328
329
330
331
332
333
334





















335
336
337
338
339
340
341
342







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+















-
-
-
+
+
+




-

-
-

-
-
-
-
-
-

-
+



-
+


-
+


-
+


-
+

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







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








	if (function == NULL)
		return false;

	function(entry, extension, encoding);
	return true;
}

static void
readExtensions(OFLHAArchiveEntry *entry, OFStream *stream,
    of_string_encoding_t encoding)
{
	uint16_t nextSize;

	while ((nextSize = [stream readLittleEndianInt16]) > 0) {
		OFData *extension;

		if (nextSize < 2)
			@throw [OFInvalidFormatException exception];

		extension = [stream readDataWithCount: nextSize - 2];

		if (!parseExtension(entry, extension, encoding))
			[entry->_extensions addObject: extension];
	}
}

@implementation OFLHAArchiveEntry
@synthesize method = _method, compressedSize = _compressedSize;
@synthesize uncompressedSize = _uncompressedSize, date = _date;
@synthesize level = _level, CRC16 = _CRC16;
@synthesize operatingSystemIdentifier = _operatingSystemIdentifier;
@synthesize fileComment = _fileComment, mode = _mode, UID = _UID, GID = _GID;
@synthesize owner = _owner, group = _group;
@synthesize modificationDate = _modificationDate, extensions = _extensions;

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)of_initWithHeaderSize: (uint8_t)headerSize
			       stream: (OFStream *)stream
			     encoding: (of_string_encoding_t)encoding
- (instancetype)of_initWithHeader: (char [21])header
			   stream: (OFStream *)stream
			 encoding: (of_string_encoding_t)encoding
{
	self = [super init];

	@try {
		char header[20];
		uint32_t date;
		uint16_t nextSize;
		OFMutableArray *extensions;

		if (headerSize < 21)
			@throw [OFInvalidFormatException exception];

		[stream readIntoBuffer: header
			   exactLength: 20];

		_method = [[OFString alloc]
		    initWithCString: header + 1
		    initWithCString: header + 2
			   encoding: OF_STRING_ENCODING_ASCII
			     length: 5];

		memcpy(&_compressedSize, header + 6, 4);
		memcpy(&_compressedSize, header + 7, 4);
		_compressedSize = OF_BSWAP32_IF_BE(_compressedSize);

		memcpy(&_uncompressedSize, header + 10, 4);
		memcpy(&_uncompressedSize, header + 11, 4);
		_uncompressedSize = OF_BSWAP32_IF_BE(_uncompressedSize);

		memcpy(&date, header + 14, 4);
		memcpy(&date, header + 15, 4);
		date = OF_BSWAP32_IF_BE(date);

		_level = header[19];
		_level = header[20];

		_extensions = [[OFMutableArray alloc] init];

		if (_level != 2) {
		switch (_level) {
		case 0:;
			void *pool = objc_autoreleasePoolPush();
			uint16_t fileNameLength = [stream readInt8];
			OFString *tmp;

			_date = [parseMSDOSDate(date) retain];

			tmp = [stream readStringWithLength: fileNameLength
						  encoding: encoding];
			tmp = [tmp stringByReplacingOccurrencesOfString: @"\\"
							     withString: @"/"];
			_fileName = [tmp copy];

			_CRC16 = [stream readLittleEndianInt16];

			objc_autoreleasePoolPop(pool);
			break;
		case 2:
			_date = [[OFDate alloc]
			    initWithTimeIntervalSince1970: date];

			_CRC16 = [stream readLittleEndianInt16];
			_operatingSystemIdentifier = [stream readInt8];

			readExtensions(self, stream, encoding);

			break;
		default:;
			OFString *version = [OFString
			    stringWithFormat: @"%u", _level];

			@throw [OFUnsupportedVersionException
			    exceptionWithVersion: version];
		}

		_date = [[OFDate alloc] initWithTimeIntervalSince1970: date];

		_CRC16 = [stream readLittleEndianInt16];
		_operatingSystemIdentifier = [stream readInt8];

		extensions = [[OFMutableArray alloc] init];
		_extensions = extensions;

		while ((nextSize = [stream readLittleEndianInt16]) > 0) {
			OFData *extension;

			if (nextSize < 2)
				@throw [OFInvalidFormatException exception];

			extension = [stream readDataWithCount: nextSize - 2];

			if (!parseExtension(self, extension, encoding))
				[extensions addObject: extension];
		}

		[extensions makeImmutable];
		[_extensions makeImmutable];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

Modified utils/ofzip/LHAArchive.m from [7bf18d4f42] to [50bf2d19a1].

66
67
68
69
70
71
72

73

74
75
76
77
78
79
80
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81







+
-
+







{
	self = [super init];

	@try {
		_archive = [[OFLHAArchive alloc] initWithStream: stream
							   mode: mode];

		if (encoding != OF_STRING_ENCODING_AUTODETECT)
		[_archive setEncoding: encoding];
			[_archive setEncoding: encoding];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
97
98
99
100
101
102
103
104

105
106
107
108
109
110
111
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112







-
+








		if (app->_outputLevel >= 1) {
			OFString *date = [[entry date]
			    localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"];
			OFString *compressedSize = [OFString stringWithFormat:
			    @"%" PRIu32, [entry compressedSize]];
			OFString *uncompressedSize = [OFString stringWithFormat:
			    @"%" PRIu32, [entry compressedSize]];
			    @"%" PRIu32, [entry uncompressedSize]];
			OFString *CRC16 = [OFString stringWithFormat:
			    @"%04" PRIX16, [entry CRC16]];

			[of_stdout writeString: @"\t"];
			[of_stdout writeLine: OF_LOCALIZED(
			    @"list_compressed_size",
			    @"Compressed: %[size] bytes",

Modified utils/ofzip/OFZIP.m from [fe40a35a63] to [047f714025].

166
167
168
169
170
171
172
173

174
175
176
177
178
179
180
166
167
168
169
170
171
172

173
174
175
176
177
178
179
180







-
+







		{ 't', @"type", 1, NULL, &type },
		{ 'v', @"verbose", 0, NULL, NULL },
		{ 'x', @"extract", 0, NULL, NULL },
		{ '\0', nil, 0, NULL, NULL }
	};
	OFOptionsParser *optionsParser;
	of_unichar_t option, mode = '\0';
	of_string_encoding_t encoding = OF_STRING_ENCODING_UTF_8;
	of_string_encoding_t encoding = OF_STRING_ENCODING_AUTODETECT;
	OFArray OF_GENERIC(OFString *) *remainingArguments, *files;
	id <Archive> archive;

#ifdef OF_HAVE_SANDBOX
	OFSandbox *sandbox = [[OFSandbox alloc] init];
	@try {
		[sandbox setAllowsStdIO: true];

Modified utils/ofzip/TarArchive.m from [f7b1262e85] to [53ccc5f3ec].

66
67
68
69
70
71
72

73

74
75
76
77
78
79
80
66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
81







+
-
+







{
	self = [super init];

	@try {
		_archive = [[OFTarArchive alloc] initWithStream: stream
							   mode: mode];

		if (encoding != OF_STRING_ENCODING_AUTODETECT)
		[_archive setEncoding: encoding];
			[_archive setEncoding: encoding];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}