ObjFW  Diff

Differences From Artifact [a29caa1d81]:

To Artifact [c17a542e4e]:


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
64
65
66

67
68
69
70
71

72
73
74
75

76
77
78
79
80
81
82
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
64
65

66
67
68
69
70

71
72
73
74

75
76
77
78
79
80
81
82







-
+

-
+







-
+

-
+










-
+




-
+



-
+







/* References for static linking */
void _references_to_categories_of_OFDataArray(void)
{
	_OFDataArray_Hashing_reference = 1;
};

@implementation OFDataArray
+ dataArrayWithItemSize: (size_t)is
+ dataArrayWithItemSize: (size_t)itemSize
{
	return [[[self alloc] initWithItemSize: is] autorelease];
	return [[[self alloc] initWithItemSize: itemSize] autorelease];
}

+ dataArrayWithContentsOfFile: (OFString*)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ dataArrayWithBase64EncodedString: (OFString*)str
+ dataArrayWithBase64EncodedString: (OFString*)string
{
	return [[[self alloc] initWithBase64EncodedString: str] autorelease];
	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- init
{
	Class c = isa;
	[self release];
	@throw [OFNotImplementedException newWithClass: c
					      selector: _cmd];
}

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

	@try {
		if (is == 0)
		if (itemSize_ == 0)
			@throw [OFInvalidArgumentException newWithClass: isa
							       selector: _cmd];

		itemSize = is;
		itemSize = itemSize_;
		data = NULL;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
90
91
92
93
94
95
96
97

98
99
100

101
102
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
132
133
134
90
91
92
93
94
95
96

97
98
99

100
101




102
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
132
133
134







-
+


-
+

-
-
-
-
+
+
+
+


-
+











-
+






-
+







		OFFile *file = [[OFFile alloc] initWithPath: path
						       mode: @"rb"];

		itemSize = 1;
		data = NULL;

		@try {
			char *buf = [self allocMemoryWithSize: of_pagesize];
			char *buffer = [self allocMemoryWithSize: of_pagesize];

			while (![file isAtEndOfStream]) {
				size_t size;
				size_t length;

				size = [file readNBytes: of_pagesize
					     intoBuffer: buf];
				[self addNItems: size
				     fromCArray: buf];
				length = [file readNBytes: of_pagesize
					       intoBuffer: buffer];
				[self addNItems: length
				     fromCArray: buffer];
			}

			[self freeMemory: buf];
			[self freeMemory: buffer];
		} @finally {
			[file release];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithBase64EncodedString: (OFString*)str
- initWithBase64EncodedString: (OFString*)string
{
	self = [super init];

	itemSize = 1;
	data = NULL;

	if (!of_base64_decode(self, [str cString], [str cStringLength])) {
	if (!of_base64_decode(self, [string cString], [string cStringLength])) {
		Class c = isa;
		[self release];
		@throw [OFInvalidEncodingException newWithClass: c];
	}

	return self;
}
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
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
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
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







-
-
+
+

-
+



-
+


-
-
+
+


-
-
+
+


-
+



-
+


-
+

-
+

-
+








-
+

-
+



-
+










-
+


-
+


-
-
+
+

-
+












-
-
-
+
+
+
+

-
+


-
+

+
+
-
+

+
+
+
-
-
+
+

-
+





-
+

+
-
-
+
+

-
+


+
+
-
+



-
-
+
+

-
-
-
+
+
+

-
+




-
+







	atIndex: (size_t)index
{
	[self addNItems: 1
	     fromCArray: item
		atIndex: index];
}

- (void)addNItems: (size_t)nitems
       fromCArray: (const void*)carray
- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
{
	if (nitems > SIZE_MAX - count)
	if (nItems > SIZE_MAX - count)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMemory: data
			 toNItems: count + nitems
			 toNItems: count + nItems
			 withSize: itemSize];

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

- (void)addNItems: (size_t)nitems
       fromCArray: (const void*)carray
- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
	  atIndex: (size_t)index
{
	if (nitems > SIZE_MAX - count)
	if (nItems > SIZE_MAX - count)
		@throw [OFOutOfRangeException newWithClass: isa];

	data = [self resizeMemory: data
			 toNItems: count + nitems
			 toNItems: count + nItems
			 withSize: itemSize];

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

	count += nitems;
	count += nItems;
}

- (void)removeItemAtIndex: (size_t)index
{
	[self removeNItems: 1
		   atIndex: index];
}

- (void)removeNItems: (size_t)nitems
- (void)removeNItems: (size_t)nItems
{
	if (nitems > count)
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];


	count -= nitems;
	count -= nItems;
	@try {
		data = [self resizeMemory: data
				 toNItems: count
				 withSize: itemSize];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- (void)removeNItems: (size_t)nitems
- (void)removeNItems: (size_t)nItems
	     atIndex: (size_t)index
{
	if (nitems > count)
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

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

	count -= nitems;
	count -= nItems;
	@try {
		data = [self resizeMemory: data
				 toNItems: count
				 withSize: itemSize];
	} @catch (OFOutOfMemoryException *e) {
		/* We don't really care, as we only made it smaller */
		[e release];
	}
}

- copy
{
	OFDataArray *new = [[OFDataArray alloc] initWithItemSize: itemSize];
	[new addNItems: count
	    fromCArray: data];
	OFDataArray *copy = [[OFDataArray alloc] initWithItemSize: itemSize];

	[copy addNItems: count
	     fromCArray: data];

	return new;
	return copy;
}

- (BOOL)isEqual: (id)obj
- (BOOL)isEqual: (id)object
{
	OFDataArray *otherDataArray;

	if (![obj isKindOfClass: [OFDataArray class]])
	if (![object isKindOfClass: [OFDataArray class]])
		return NO;

	otherDataArray = (OFDataArray*)object;

	if ([(OFDataArray*)obj count] != count ||
	    [(OFDataArray*)obj itemSize] != itemSize)
	if ([otherDataArray count] != count ||
	    [otherDataArray itemSize] != itemSize)
		return NO;
	if (memcmp([(OFDataArray*)obj cArray], data, count * itemSize))
	if (memcmp([otherDataArray cArray], data, count * itemSize))
		return NO;

	return YES;
}

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

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

	if ([(OFDataArray*)obj itemSize] != itemSize)
	if ([otherDataArray itemSize] != itemSize)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	ary_count = [(OFDataArray*)obj count];
	min_count = (count > ary_count ? ary_count : count);
	otherCount = [otherDataArray count];
	minimumCount = (count > otherCount ? otherCount : count);

	if ((cmp = memcmp(data, [(OFDataArray*)obj cArray],
	    min_count * itemSize)) == 0) {
		if (count > ary_count)
	if ((comparison = memcmp(data, [otherDataArray cArray],
	    minimumCount * itemSize)) == 0) {
		if (count > otherCount)
			return OF_ORDERED_DESCENDING;
		if (count < ary_count)
		if (count < otherCount)
			return OF_ORDERED_ASCENDING;
		return OF_ORDERED_SAME;
	}

	if (cmp > 0)
	if (comparison > 0)
		return OF_ORDERED_DESCENDING;
	else
		return OF_ORDERED_ASCENDING;
}

- (uint32_t)hash
{
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
364
365


366
367

368
369

370
371
372
373


374
375

376
377

378
379

380
381
382


383
384
385
386


387
388
389

390
391

392
393
394
395


396
397

398
399

400
401
402

403
404

405
406
407


408
409
410

411
412

413
414

415
416
417
418
419



420
421

422
423
424


425
426
427

428
429
430

431
432

433
434
435
436


437
438
439
440



441
442

443
444
445


446
447
448
449
450

451
452
453


454
455

456
457
348
349
350
351
352
353
354

355
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


381
382
383

384
385

386
387

388
389


390
391
392
393


394
395
396
397

398
399

400
401
402


403
404
405

406
407

408
409
410

411
412

413
414


415
416
417
418

419
420

421
422

423
424
425



426
427
428
429

430
431


432
433
434
435

436
437
438

439
440

441
442
443


444
445
446



447
448
449
450

451
452


453
454
455
456
457
458

459
460


461
462
463

464
465
466







-
+




-
-
+
+

-
+

-
+




-
+


-
-
+
+

-
+

-
+


-
-
+
+

-
+

-
+

-
+

-
-
+
+


-
-
+
+


-
+

-
+


-
-
+
+

-
+

-
+


-
+

-
+

-
-
+
+


-
+

-
+

-
+


-
-
-
+
+
+

-
+

-
-
+
+


-
+


-
+

-
+


-
-
+
+

-
-
-
+
+
+

-
+

-
-
+
+




-
+

-
-
+
+

-
+


	return of_base64_encode(data, count * itemSize);
}
@end

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

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

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

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

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

	count++;
	size = nsize;
	size = newSize;
}

- (void)addNItems: (size_t)nitems
       fromCArray: (const void*)carray
- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
{
	size_t nsize, lastpagebyte;
	size_t newSize, lastPageByte;

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

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

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

	memcpy(data + count * itemSize, carray, nitems * itemSize);
	memcpy(data + count * itemSize, cArray, nItems * itemSize);

	count += nitems;
	size = nsize;
	count += nItems;
	size = newSize;
}

- (void)addNItems: (size_t)nitems
       fromCArray: (const void*)carray
- (void)addNItems: (size_t)nItems
       fromCArray: (const void*)cArray
	  atIndex: (size_t)index
{
	size_t nsize, lastpagebyte;
	size_t newSize, lastPageByte;

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

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

	if (size != nsize)
	if (size != newSize)
		data = [self resizeMemory: data
				 toNItems: nsize
				 toNItems: newSize
				 withSize: itemSize];

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

	count += nitems;
	size = nsize;
	count += nItems;
	size = newSize;
}

- (void)removeNItems: (size_t)nitems
- (void)removeNItems: (size_t)nItems
{
	size_t nsize, lastpagebyte;
	size_t newSize, lastPageByte;

	if (nitems > count)
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

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

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

- (void)removeNItems: (size_t)nitems
- (void)removeNItems: (size_t)nItems
	     atIndex: (size_t)index
{
	size_t nsize, lastpagebyte;
	size_t newSize, lastPageByte;

	if (nitems > count)
	if (nItems > count)
		@throw [OFOutOfRangeException newWithClass: isa];

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

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

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

- copy
{
	OFDataArray *new = [[OFBigDataArray alloc] initWithItemSize: itemSize];
	OFDataArray *copy = [[OFBigDataArray alloc] initWithItemSize: itemSize];

	[new addNItems: count
	    fromCArray: data];
	[copy addNItems: count
	     fromCArray: data];

	return new;
	return copy;
}
@end