ObjFW  Diff

Differences From Artifact [a3f3ece2b2]:

To Artifact [3687781c21]:


80
81
82
83
84
85
86
87

88
89
90
91
92
93
94
80
81
82
83
84
85
86

87
88
89
90
91
92
93
94







-
+







}

- initWithItemSize: (size_t)itemSize_
{
	self = [super init];

	if (itemSize_ == 0) {
		Class c = isa;
		Class c = [self class];
		[self release];
		@throw [OFInvalidArgumentException exceptionWithClass: c
							     selector: _cmd];
	}

	itemSize = itemSize_;

132
133
134
135
136
137
138
139

140
141
142
143
144
145
146
132
133
134
135
136
137
138

139
140
141
142
143
144
145
146







-
+







- initWithContentsOfURL: (OFURL*)URL
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	Class c;

	c = isa;
	c = [self class];
	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	if ([[URL scheme] isEqual: @"file"]) {
		self = [[c alloc] initWithContentsOfFile: [URL path]];
		[pool release];
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
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







-
+




















-
+









-
+







	self = [super init];

	itemSize = 1;

	if (!of_base64_decode(self,
	    [string cStringWithEncoding: OF_STRING_ENCODING_ASCII],
	    [string cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII])) {
		Class c = isa;
		Class c = [self class];
		[self release];
		@throw [OFInvalidEncodingException exceptionWithClass: c];
	}

	return self;
}

- initWithSerialization: (OFXMLElement*)element
{
	self = [super init];

	itemSize = 1;

	@try {
		OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
		OFString *stringValue;

		if (![[element name] isEqual: [self className]] ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
			    exceptionWithClass: [self class]
				      selector: _cmd];

		stringValue = [element stringValue];

		if (!of_base64_decode(self,
		    [stringValue cStringWithEncoding: OF_STRING_ENCODING_ASCII],
		    [stringValue cStringLengthWithEncoding:
		    OF_STRING_ENCODING_ASCII]))
			@throw [OFInvalidEncodingException
			    exceptionWithClass: isa];
			    exceptionWithClass: [self class]];

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

226
227
228
229
230
231
232
233

234
235
236
237
238
239
240
226
227
228
229
230
231
232

233
234
235
236
237
238
239
240







-
+







{
	return data;
}

- (void*)itemAtIndex: (size_t)index
{
	if (index >= count)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	return data + index * itemSize;
}

- (void*)firstItem
{
	if (data == NULL || count == 0)
250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
250
251
252
253
254
255
256

257
258
259
260
261
262
263
264







-
+








	return data + (count - 1) * itemSize;
}

- (void)addItem: (const void*)item
{
	if (SIZE_MAX - count < 1)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	data = [self resizeMemory: data
			     size: itemSize
			    count: count + 1];

	memcpy(data + count * itemSize, item, itemSize);

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







-
+














-
+




















-
+


















-
+







			      count: 1];
}

- (void)addItemsFromCArray: (const void*)cArray
		     count: (size_t)nItems
{
	if (nItems > SIZE_MAX - count)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	data = [self resizeMemory: data
			     size: itemSize
			    count: count + nItems];

	memcpy(data + count * itemSize, cArray, nItems * itemSize);
	count += nItems;
}

- (void)insertItemsFromCArray: (const void*)cArray
		      atIndex: (size_t)index
			count: (size_t)nItems
{
	if (nItems > SIZE_MAX - count || index > count)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	data = [self resizeMemory: data
			     size: itemSize
			    count: count + nItems];

	memmove(data + (index + nItems) * itemSize, data + index * itemSize,
	    (count - index) * itemSize);
	memcpy(data + index * itemSize, cArray, nItems * itemSize);

	count += nItems;
}

- (void)removeItemAtIndex: (size_t)index
{
	[self removeItemsInRange: of_range(index, 1)];
}

- (void)removeItemsInRange: (of_range_t)range
{
	if (range.start + range.length > count)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	memmove(data + range.start * itemSize,
	    data + (range.start + range.length) * itemSize,
	    (count - range.start - range.length) * itemSize);

	count -= range.length;
	@try {
		data = [self resizeMemory: data
				     size: itemSize
				    count: count];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
	}
}

- (void)removeLastItem
{
	if (count < 1)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	count--;
	@try {
		data = [self resizeMemory: data
				     size: itemSize
				    count: count];
	} @catch (OFOutOfMemoryException *e) {
350
351
352
353
354
355
356
357

358
359
360
361
362
363
364
350
351
352
353
354
355
356

357
358
359
360
361
362
363
364







-
+








	data = NULL;
	count = 0;
}

- copy
{
	OFDataArray *copy = [[isa alloc] initWithItemSize: itemSize];
	OFDataArray *copy = [[[self class] alloc] initWithItemSize: itemSize];

	[copy addItemsFromCArray: data
			   count: count];

	return copy;
}

383
384
385
386
387
388
389
390
391



392
393
394
395
396



397
398
399
400
401
402
403
383
384
385
386
387
388
389


390
391
392
393
394
395


396
397
398
399
400
401
402
403
404
405







-
-
+
+
+



-
-
+
+
+







- (of_comparison_result_t)compare: (id)object
{
	OFDataArray *otherDataArray;
	int comparison;
	size_t otherCount, minimumCount;

	if (![object isKindOfClass: [OFDataArray class]])
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];
	otherDataArray = object;

	if ([otherDataArray itemSize] != itemSize)
		@throw [OFInvalidArgumentException exceptionWithClass: isa
							     selector: _cmd];
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	otherCount = [otherDataArray count];
	minimumCount = (count > otherCount ? otherCount : count);

	if ((comparison = memcmp(data, [otherDataArray cArray],
	    minimumCount * itemSize)) == 0) {
		if (count > otherCount)
446
447
448
449
450
451
452
453
454



455
456
457
458
459
460
461
448
449
450
451
452
453
454


455
456
457
458
459
460
461
462
463
464







-
-
+
+
+








- (OFXMLElement*)XMLElementBySerializing
{
	OFAutoreleasePool *pool;
	OFXMLElement *element;

	if (itemSize != 1)
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	pool = [[OFAutoreleasePool alloc] init];
	element = [OFXMLElement
	    elementWithName: [self className]
		  namespace: OF_SERIALIZATION_NS
		stringValue: of_base64_encode(data, count * itemSize)];

469
470
471
472
473
474
475
476

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

498
499
500
501
502
503
504
472
473
474
475
476
477
478

479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

500
501
502
503
504
505
506
507







-
+




















-
+








@implementation OFBigDataArray
- (void)addItem: (const void*)item
{
	size_t newSize, lastPageByte;

	if (SIZE_MAX - count < 1 || count + 1 > SIZE_MAX / itemSize)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	lastPageByte = of_pagesize - 1;
	newSize = ((count + 1) * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				     size: newSize];

	memcpy(data + count * itemSize, item, itemSize);

	count++;
	size = newSize;
}

- (void)addItemsFromCArray: (const void*)cArray
		     count: (size_t)nItems
{
	size_t newSize, lastPageByte;

	if (nItems > SIZE_MAX - count || count + nItems > SIZE_MAX / itemSize)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	lastPageByte = of_pagesize - 1;
	newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				     size: newSize];
513
514
515
516
517
518
519
520

521
522
523
524
525
526
527
516
517
518
519
520
521
522

523
524
525
526
527
528
529
530







-
+







		      atIndex: (size_t)index
			count: (size_t)nItems
{
	size_t newSize, lastPageByte;

	if (nItems > SIZE_MAX - count || index > count ||
	    count + nItems > SIZE_MAX / itemSize)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	lastPageByte = of_pagesize - 1;
	newSize = ((count + nItems) * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				     size: newSize];
535
536
537
538
539
540
541
542

543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563

564
565
566
567
568
569
570
538
539
540
541
542
543
544

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565

566
567
568
569
570
571
572
573







-
+




















-
+







}

- (void)removeItemsInRange: (of_range_t)range
{
	size_t newSize, lastPageByte;

	if (range.start + range.length > count)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	memmove(data + range.start * itemSize,
	    data + (range.start + range.length) * itemSize,
	    (count - range.start - range.length) * itemSize);

	count -= range.length;
	lastPageByte = of_pagesize - 1;
	newSize = (count * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize)
		data = [self resizeMemory: data
				     size: newSize];
	size = newSize;
}

- (void)removeLastItem
{
	size_t newSize, lastPageByte;

	if (count < 1)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	count--;
	lastPageByte = of_pagesize - 1;
	newSize = (count * itemSize + lastPageByte) & ~lastPageByte;

	if (size != newSize) {
		@try {