ObjFW  Diff

Differences From Artifact [468c0a34a5]:

To Artifact [c84ec47cce]:


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
94
95
96
97
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
94
95
96
97







-

+


+
-
+
-












-

+



-

+







			count: (size_t)count
{
	return [[[self alloc] initWithItems: items
				      count: count] autorelease];
}

+ (instancetype)dataWithItems: (const void *)items
		     itemSize: (size_t)itemSize
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	return [[[self alloc] initWithItems: items
				      count: count
				   itemSize: itemSize
				   itemSize: itemSize] autorelease];
				      count: count] autorelease];
}

+ (instancetype)dataWithItemsNoCopy: (void *)items
			      count: (size_t)count
		       freeWhenDone: (bool)freeWhenDone
{
	return [[[self alloc] initWithItemsNoCopy: items
					    count: count
				     freeWhenDone: freeWhenDone] autorelease];
}

+ (instancetype)dataWithItemsNoCopy: (void *)items
			   itemSize: (size_t)itemSize
			      count: (size_t)count
			   itemSize: (size_t)itemSize
		       freeWhenDone: (bool)freeWhenDone
{
	return [[[self alloc] initWithItemsNoCopy: items
					 itemSize: itemSize
					    count: count
					 itemSize: itemSize
				     freeWhenDone: freeWhenDone] autorelease];
}

#ifdef OF_HAVE_FILES
+ (instancetype)dataWithContentsOfFile: (OFString *)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
114
115
116
117
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
114
115
116
117
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







+
-
+
-



-

+







-
-
+
+

-
+















-

+




-

+









-

+







	return [[[self alloc] initWithBase64EncodedString: string] autorelease];
}

- (instancetype)initWithItems: (const void *)items
			count: (size_t)count
{
	return [self initWithItems: items
			     count: count
			  itemSize: 1
			  itemSize: 1];
			     count: count];
}

- (instancetype)initWithItems: (const void *)items
		     itemSize: (size_t)itemSize
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super init];

	@try {
		if (itemSize == 0)
			@throw [OFInvalidArgumentException exception];

		_items = [self allocMemoryWithSize: itemSize
					     count: count];
		_items = of_malloc(count, itemSize);
		_count = count;
		_itemSize = itemSize;
		_count = count;
		_freeWhenDone = true;

		memcpy(_items, items, count * itemSize);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithItemsNoCopy: (void *)items
			      count: (size_t)count
		       freeWhenDone: (bool)freeWhenDone
{
	return [self initWithItemsNoCopy: items
				itemSize: 1
				   count: count
				itemSize: 1
			    freeWhenDone: freeWhenDone];
}

- (instancetype)initWithItemsNoCopy: (void *)items
			   itemSize: (size_t)itemSize
			      count: (size_t)count
			   itemSize: (size_t)itemSize
		       freeWhenDone: (bool)freeWhenDone
{
	self = [super init];

	@try {
		if (itemSize == 0)
			@throw [OFInvalidArgumentException exception];

		_items = (unsigned char *)items;
		_itemSize = itemSize;
		_count = count;
		_itemSize = itemSize;
		_freeWhenDone = freeWhenDone;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
192
193
194
195
196
197
198
199

200
201
202
203
204
205
206
207
208
209
192
193
194
195
196
197
198

199



200
201
202
203
204
205
206







-
+
-
-
-







		    attributesOfItemAtPath: path].fileSize;

# if ULLONG_MAX > SIZE_MAX
		if (size > SIZE_MAX)
			@throw [OFOutOfRangeException exception];
# endif

		if ((buffer = malloc((size_t)size)) == NULL)
		buffer = of_malloc((size_t)size, 1);
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: (size_t)size];

		file = [[OFFile alloc] initWithPath: path
					       mode: @"r"];
		@try {
			[file readIntoBuffer: buffer
				 exactLength: (size_t)size];
		} @finally {
			[file release];
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
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







+

-
+


-
+

+
-
-
-
+
+
+
+

-
-
+
+
+

-
-
-
-
+
+
+
+
+
+







		if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
			@throw [OFUnsupportedProtocolException
			    exceptionWithURL: URL];

		stream = [URLHandler openItemAtURL: URL
					      mode: @"r"];

		_count = 0;
		_itemSize = 1;
		_count = 0;
		_freeWhenDone = true;

		pageSize = [OFSystemInfo pageSize];
		buffer = [self allocMemoryWithSize: pageSize];
		buffer = of_malloc(1, pageSize);

		@try {
		while (!stream.atEndOfStream) {
			size_t length = [stream readIntoBuffer: buffer
							length: pageSize];
			while (!stream.atEndOfStream) {
				size_t length = [stream
				    readIntoBuffer: buffer
					    length: pageSize];

			if (SIZE_MAX - _count < length)
				@throw [OFOutOfRangeException exception];
				if (SIZE_MAX - _count < length)
					@throw [OFOutOfRangeException
					    exception];

			_items = [self resizeMemory: _items
					       size: _count + length];
			memcpy(_items + _count, buffer, length);
			_count += length;
				_items = of_realloc(_items, _count + length, 1);
				memcpy(_items + _count, buffer, length);
				_count += length;
			}
		} @finally {
			free(buffer);
		}

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}
284
285
286
287
288
289
290
291


292
293

294
295
296
297
298
299
300
287
288
289
290
291
292
293

294
295
296

297
298
299
300
301
302
303
304







-
+
+

-
+







		const char *cString;

		if (count % 2 != 0)
			@throw [OFInvalidFormatException exception];

		count /= 2;

		_items = [self allocMemoryWithSize: count];
		_items = of_malloc(count, 1);
		_count = count;
		_itemSize = 1;
		_count = count;
		_freeWhenDone = true;

		cString = [string
		    cStringWithEncoding: OF_STRING_ENCODING_ASCII];

		for (size_t i = 0; i < count; i++) {
			uint8_t c1 = cString[2 * i];
			uint8_t c2 = cString[2 * i + 1];
427
428
429
430
431
432
433

434

435
436
437
438
439
440
441
442
431
432
433
434
435
436
437
438

439

440
441
442
443
444
445
446







+
-
+
-







{
	return [self retain];
}

- (id)mutableCopy
{
	return [[OFMutableData alloc] initWithItems: _items
					      count: _count
					   itemSize: _itemSize
					   itemSize: _itemSize];
					      count: _count];
}

- (bool)isEqual: (id)object
{
	OFData *data;

	if (object == self)
484
485
486
487
488
489
490
491

492
493
494
495
496
497
498
488
489
490
491
492
493
494

495
496
497
498
499
500
501
502







-
+








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

- (uint32_t)hash
- (unsigned long)hash
{
	uint32_t hash;

	OF_HASH_INIT(hash);

	for (size_t i = 0; i < _count * _itemSize; i++)
		OF_HASH_ADD(hash, ((uint8_t *)_items)[i]);
507
508
509
510
511
512
513
514
515

516
517
518
519
520
521
522
511
512
513
514
515
516
517

518
519
520
521
522
523
524
525
526







-

+







	OFData *ret;

	if (range.length > SIZE_MAX - range.location ||
	    range.location + range.length > _count)
		@throw [OFOutOfRangeException exception];

	ret = [OFData dataWithItemsNoCopy: _items + (range.location * _itemSize)
				 itemSize: _itemSize
				    count: range.length
				 itemSize: _itemSize
			     freeWhenDone: false];
	ret->_parentData = [(_parentData != nil ? _parentData : self) copy];

	return ret;
}

- (OFString *)description