ObjFW  Diff

Differences From Artifact [a40561e369]:

To Artifact [0481cd7f61]:


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
85

86
87
88
89
90
91
92
93
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
85
86
87
88







-
+
-

-
+
-



-
+
-

-
+
-








-
+
-







- (instancetype)of_initWithStream: (OFStream *)stream
			    entry: (OFTarArchiveEntry *)entry;
@end

@implementation OFTarArchive: OFObject
@synthesize encoding = _encoding;

+ (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 {
		_stream = [stream retain];

		if ([mode isEqual: @"r"])
104
105
106
107
108
109
110
111

112
113
114
115
116
117
118
119
99
100
101
102
103
104
105

106

107
108
109
110
111
112
113







-
+
-







			bool empty = true;

			if (![_stream isKindOfClass: [OFSeekableStream class]])
				@throw [OFInvalidArgumentException exception];

			[(OFSeekableStream *)_stream seekToOffset: -1024
							   whence: SEEK_END];
			[_stream readIntoBuffer: buffer
			[_stream readIntoBuffer: buffer exactLength: 1024];
				    exactLength: 1024];

			for (size_t i = 0; i < 1024 / sizeof(uint32_t); i++)
				if (buffer[i] != 0)
					empty = false;

			if (!empty)
				@throw [OFInvalidFormatException exception];
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
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







-
+
-




-
+
-

-
+
-


-
+
-







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

187
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
169
170
171
172
173
174
175

176

177
178
179
180
181
182

183

184
185
186
187
188
189
190







-
+
-






-
+
-







	}
	[_lastReturnedStream release];
	_lastReturnedStream = nil;

	if (_stream.atEndOfStream)
		return nil;

	[_stream readIntoBuffer: buffer
	[_stream readIntoBuffer: buffer exactLength: 512];
		    exactLength: 512];

	for (size_t i = 0; i < 512 / sizeof(uint32_t); i++)
		if (buffer[i] != 0)
			empty = false;

	if (empty) {
		[_stream readIntoBuffer: buffer
		[_stream readIntoBuffer: buffer exactLength: 512];
			    exactLength: 512];

		for (size_t i = 0; i < 512 / sizeof(uint32_t); i++)
			if (buffer[i] != 0)
				@throw [OFInvalidFormatException exception];

		return nil;
	}
238
239
240
241
242
243
244
245

246
247
248
249
250
251
252
253
226
227
228
229
230
231
232

233

234
235
236
237
238
239
240







-
+
-







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

	[entry of_writeToStream: _stream
	[entry of_writeToStream: _stream encoding: _encoding];
		       encoding: _encoding];

	_lastReturnedStream = [[OFTarArchiveFileWriteStream alloc]
	    of_initWithStream: _stream
			entry: entry];

	objc_autoreleasePoolPop(pool);

324
325
326
327
328
329
330
331

332
333
334
335
336
337
338
339
340
311
312
313
314
315
316
317

318


319
320
321
322
323
324
325







-
+
-
-







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

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

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

	if (ret == 0)
		_atEndOfStream = true;

	_toRead -= ret;

	return ret;
}
392
393
394
395
396
397
398
399

400
401
402
403
404
405
406
407
377
378
379
380
381
382
383

384

385
386
387
388
389
390
391







-
+
-







			    seekToOffset: 512 - (size % 512)
				  whence: SEEK_CUR];
	} else {
		char buffer[512];
		uint64_t size;

		while (_toRead >= 512) {
			[_stream readIntoBuffer: buffer
			[_stream readIntoBuffer: buffer exactLength: 512];
				    exactLength: 512];
			_toRead -= 512;
		}

		if (_toRead > 0) {
			[_stream readIntoBuffer: buffer
				    exactLength: (size_t)_toRead];
			_toRead = 0;
442
443
444
445
446
447
448
449

450
451
452
453
454
455
456
457
426
427
428
429
430
431
432

433

434
435
436
437
438
439
440







-
+
-







		[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 (_stream == nil)
		@throw [OFNotOpenException exceptionWithObject: self];

	if ((uint64_t)length > _toWrite)