ObjFW  Check-in [181edbb0f0]

Overview
Comment:OFZooArchive: Add support for archive comments
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 181edbb0f031e6c98a7a362b9c3bdfafc4f48ac2426d43276728c6fd698b0706
User & Date: js on 2024-03-09 11:50:08
Other Links: manifest | tags
Context
2024-03-09
12:10
ofarc: Print archive comment with `-lv` check-in: f5bc11fc39 user: js tags: trunk
11:50
OFZooArchive: Add support for archive comments check-in: 181edbb0f0 user: js tags: trunk
2024-03-06
20:56
Add a little humor check-in: fdd9519428 user: js tags: trunk
Changes

Modified src/OFZooArchive.h from [f7452af878] to [019d7fe0e7].

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
@interface OFZooArchive: OFObject
{
	OF_KINDOF(OFStream *) _stream;
	uint_least8_t _mode;
	OFStringEncoding _encoding;
	uint16_t _minVersionNeeded;
	uint8_t _headerType;

	OFZooArchiveEntry *_Nullable _currentEntry;
#ifdef OF_ZOO_ARCHIVE_M
@public
#endif
	OFStream *_Nullable _lastReturnedStream;
@protected
	OFStreamOffset _lastHeaderOffset;
	size_t _lastHeaderLength;
}

/**
 * @brief The encoding to use for the archive. Defaults to UTF-8.
 */
@property (nonatomic) OFStringEncoding encoding;






/**
 * @brief Creates a new OFZooArchive object with the specified stream.
 *
 * @param stream A stream from which the Zoo archive will be read.
 *		 This needs to be an OFSeekableStream. For writing, the stream
 *		 needs to support both reading and writing at the same time.
 * @param mode The mode for the Zoo file. Valid modes are "r" for reading and







>















>
>
>
>
>







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
59
60
61
62
63
@interface OFZooArchive: OFObject
{
	OF_KINDOF(OFStream *) _stream;
	uint_least8_t _mode;
	OFStringEncoding _encoding;
	uint16_t _minVersionNeeded;
	uint8_t _headerType;
	OFString *_Nullable _archiveComment;
	OFZooArchiveEntry *_Nullable _currentEntry;
#ifdef OF_ZOO_ARCHIVE_M
@public
#endif
	OFStream *_Nullable _lastReturnedStream;
@protected
	OFStreamOffset _lastHeaderOffset;
	size_t _lastHeaderLength;
}

/**
 * @brief The encoding to use for the archive. Defaults to UTF-8.
 */
@property (nonatomic) OFStringEncoding encoding;

/**
 * @brief The archive comment.
 */
@property OF_NULLABLE_PROPERTY (copy, nonatomic) OFString *archiveComment;

/**
 * @brief Creates a new OFZooArchive object with the specified stream.
 *
 * @param stream A stream from which the Zoo archive will be read.
 *		 This needs to be an OFSeekableStream. For writing, the stream
 *		 needs to support both reading and writing at the same time.
 * @param mode The mode for the Zoo file. Valid modes are "r" for reading and

Modified src/OFZooArchive.m from [c337e995c2] to [b281cdb8f3].

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
}

- (void)dealloc
{
	if (_stream != nil)
		[self close];


	[_currentEntry release];

	[super dealloc];
}

- (void)of_readArchiveHeader
{
	char headerText[20];
	uint32_t firstFileOffset;


	[_stream readIntoBuffer: headerText exactLength: 20];

	if ([_stream readLittleEndianInt32] != 0xFDC4A7DC)
		@throw [OFInvalidFormatException exception];

	firstFileOffset = [_stream readLittleEndianInt32];

	if ([_stream readLittleEndianInt32] != ~(uint32_t)(firstFileOffset - 1))
		@throw [OFInvalidFormatException exception];

	if ((_minVersionNeeded = [_stream readBigEndianInt16]) > 0x201)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%" PRIu8 @".%" PRIu8,
						_minVersionNeeded >> 8,
						_minVersionNeeded & 0xFF]];

	if ((_headerType = [_stream readInt8]) > 1)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%" PRIu8, _headerType]];










	[_stream seekToOffset: firstFileOffset whence: OFSeekSet];
}





















- (OFZooArchiveEntry *)nextEntry
{
	if (_mode != modeRead)
		@throw [OFInvalidArgumentException exception];

	if (_currentEntry != nil)







>








|
>





















>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
236
237
238
239
240
241
242
243
244
}

- (void)dealloc
{
	if (_stream != nil)
		[self close];

	[_archiveComment release];
	[_currentEntry release];

	[super dealloc];
}

- (void)of_readArchiveHeader
{
	char headerText[20];
	uint32_t firstFileOffset, commentOffset;
	uint16_t commentLength;

	[_stream readIntoBuffer: headerText exactLength: 20];

	if ([_stream readLittleEndianInt32] != 0xFDC4A7DC)
		@throw [OFInvalidFormatException exception];

	firstFileOffset = [_stream readLittleEndianInt32];

	if ([_stream readLittleEndianInt32] != ~(uint32_t)(firstFileOffset - 1))
		@throw [OFInvalidFormatException exception];

	if ((_minVersionNeeded = [_stream readBigEndianInt16]) > 0x201)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%" PRIu8 @".%" PRIu8,
						_minVersionNeeded >> 8,
						_minVersionNeeded & 0xFF]];

	if ((_headerType = [_stream readInt8]) > 1)
		@throw [OFUnsupportedVersionException exceptionWithVersion:
		    [OFString stringWithFormat: @"%" PRIu8, _headerType]];

	commentOffset = [_stream readLittleEndianInt32];
	commentLength = [_stream readLittleEndianInt16];

	if (commentOffset > 0) {
		[_stream seekToOffset: commentOffset whence: OFSeekSet];
		_archiveComment = [_stream readStringWithLength: commentLength
						       encoding: _encoding];
	}

	[_stream seekToOffset: firstFileOffset whence: OFSeekSet];
}

- (OFString *)archiveComment
{
	return _archiveComment;
}

- (void)setArchiveComment: (OFString *)comment
{
	void *pool = objc_autoreleasePoolPush();
	OFString *old;

	if ([comment cStringLengthWithEncoding: _encoding] > UINT16_MAX)
		@throw [OFOutOfRangeException exception];

	old = _archiveComment;
	_archiveComment = [comment copy];
	[old release];

	objc_autoreleasePoolPop(pool);
}

- (OFZooArchiveEntry *)nextEntry
{
	if (_mode != modeRead)
		@throw [OFInvalidArgumentException exception];

	if (_currentEntry != nil)
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
		@throw [OFInvalidArgumentException exception];

	if (entry.compressionMethod != 0)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	if (_lastHeaderOffset == 0) {



		/* First file - write header. */
		[_stream writeBuffer: "ObjFW Zoo Archive.\x1F" length: 20];
		[_stream writeLittleEndianInt32: 0xFDC4A7DC];
		[_stream writeLittleEndianInt32: 42];
		[_stream writeLittleEndianInt32: -42];
		/* TODO: Increase to 0x201 once we add compressed files. */
		[_stream writeBigEndianInt16: 0x200];
		/* Header type */
		[_stream writeInt8: 1];
		/* Archive comment offset */

		[_stream writeLittleEndianInt32: 0];
		/* Archive comment length */


		[_stream writeLittleEndianInt16: 0];


		/* Version flag */
		[_stream writeInt8: 0];




	} else
		[self of_fixUpLastHeader];

	@try {
		[_lastReturnedStream close];
	} @catch (OFNotOpenException *e) {
		/* Might have already been closed by the user - that's fine. */







>
>
>



|
|




|
>
|
|
>
>
|
>
>


>
>
>
>







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

	if (entry.compressionMethod != 0)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

	if (_lastHeaderOffset == 0) {
		uint16_t commentLength =
		    [_archiveComment cStringLengthWithEncoding: _encoding];

		/* First file - write header. */
		[_stream writeBuffer: "ObjFW Zoo Archive.\x1F" length: 20];
		[_stream writeLittleEndianInt32: 0xFDC4A7DC];
		[_stream writeLittleEndianInt32: 42 + commentLength];
		[_stream writeLittleEndianInt32: -(42 + commentLength)];
		/* TODO: Increase to 0x201 once we add compressed files. */
		[_stream writeBigEndianInt16: 0x200];
		/* Header type */
		[_stream writeInt8: 1];

		if (_archiveComment != nil) {
			[_stream writeLittleEndianInt32: 42];
			[_stream writeLittleEndianInt16: commentLength];
		} else {
			[_stream writeLittleEndianInt32: 0];
			[_stream writeLittleEndianInt16: 0];
		}

		/* Version flag */
		[_stream writeInt8: 0];

		if (_archiveComment != nil)
			[_stream writeString: _archiveComment
				    encoding: _encoding];
	} else
		[self of_fixUpLastHeader];

	@try {
		[_lastReturnedStream close];
	} @catch (OFNotOpenException *e) {
		/* Might have already been closed by the user - that's fine. */