| ︙ | | | ︙ | |
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
{
self = [super init];
@try {
if (itemSize == 0)
@throw [OFInvalidArgumentException exception];
_items = of_alloc(count, itemSize);
_count = count;
_itemSize = itemSize;
_freeWhenDone = true;
memcpy(_items, items, count * itemSize);
} @catch (id e) {
[self release];
|
|
|
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
{
self = [super init];
@try {
if (itemSize == 0)
@throw [OFInvalidArgumentException exception];
_items = OFAllocMemory(count, itemSize);
_count = count;
_itemSize = itemSize;
_freeWhenDone = true;
memcpy(_items, items, count * itemSize);
} @catch (id e) {
[self release];
|
| ︙ | | | ︙ | |
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
|
attributesOfItemAtPath: path].fileSize;
# if ULLONG_MAX > SIZE_MAX
if (size > SIZE_MAX)
@throw [OFOutOfRangeException exception];
# endif
buffer = of_alloc((size_t)size, 1);
file = [[OFFile alloc] initWithPath: path mode: @"r"];
@try {
[file readIntoBuffer: buffer exactLength: (size_t)size];
} @finally {
[file release];
}
} @catch (id e) {
free(buffer);
[self release];
@throw e;
}
@try {
self = [self initWithItemsNoCopy: buffer
count: (size_t)size
freeWhenDone: true];
} @catch (id e) {
free(buffer);
@throw e;
}
return self;
}
#endif
|
|
|
|
|
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
|
attributesOfItemAtPath: path].fileSize;
# if ULLONG_MAX > SIZE_MAX
if (size > SIZE_MAX)
@throw [OFOutOfRangeException exception];
# endif
buffer = OFAllocMemory((size_t)size, 1);
file = [[OFFile alloc] initWithPath: path mode: @"r"];
@try {
[file readIntoBuffer: buffer exactLength: (size_t)size];
} @finally {
[file release];
}
} @catch (id e) {
OFFreeMemory(buffer);
[self release];
@throw e;
}
@try {
self = [self initWithItemsNoCopy: buffer
count: (size_t)size
freeWhenDone: true];
} @catch (id e) {
OFFreeMemory(buffer);
@throw e;
}
return self;
}
#endif
|
| ︙ | | | ︙ | |
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
|
stream = [URLHandler openItemAtURL: URL mode: @"r"];
_count = 0;
_itemSize = 1;
_freeWhenDone = true;
pageSize = [OFSystemInfo pageSize];
buffer = of_alloc(1, pageSize);
@try {
while (!stream.atEndOfStream) {
size_t length = [stream
readIntoBuffer: buffer
length: pageSize];
if (SIZE_MAX - _count < length)
@throw [OFOutOfRangeException
exception];
_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;
}
return self;
}
- (instancetype)initWithStringRepresentation: (OFString *)string
{
self = [super init];
@try {
size_t count = [string
cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII];
const char *cString;
if (count % 2 != 0)
@throw [OFInvalidFormatException exception];
count /= 2;
_items = of_alloc(count, 1);
_count = count;
_itemSize = 1;
_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];
uint8_t byte;
if (c1 >= '0' && c1 <= '9')
|
|
>
|
|
|
|
|
<
|
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
|
stream = [URLHandler openItemAtURL: URL mode: @"r"];
_count = 0;
_itemSize = 1;
_freeWhenDone = true;
pageSize = [OFSystemInfo pageSize];
buffer = OFAllocMemory(1, pageSize);
@try {
while (!stream.atEndOfStream) {
size_t length = [stream
readIntoBuffer: buffer
length: pageSize];
if (SIZE_MAX - _count < length)
@throw [OFOutOfRangeException
exception];
_items = OFResizeMemory(_items,
_count + length, 1);
memcpy(_items + _count, buffer, length);
_count += length;
}
} @finally {
OFFreeMemory(buffer);
}
objc_autoreleasePoolPop(pool);
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (instancetype)initWithStringRepresentation: (OFString *)string
{
self = [super init];
@try {
size_t count = [string
cStringLengthWithEncoding: OFStringEncodingASCII];
const char *cString;
if (count % 2 != 0)
@throw [OFInvalidFormatException exception];
count /= 2;
_items = OFAllocMemory(count, 1);
_count = count;
_itemSize = 1;
_freeWhenDone = true;
cString = [string cStringWithEncoding: OFStringEncodingASCII];
for (size_t i = 0; i < count; i++) {
uint8_t c1 = cString[2 * i];
uint8_t c2 = cString[2 * i + 1];
uint8_t byte;
if (c1 >= '0' && c1 <= '9')
|
| ︙ | | | ︙ | |
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
self = [OFMutableData alloc];
}
self = [(OFMutableData *)self initWithCapacity: string.length / 3];
@try {
if (!of_base64_decode((OFMutableData *)self,
[string cStringWithEncoding: OF_STRING_ENCODING_ASCII],
[string cStringLengthWithEncoding:
OF_STRING_ENCODING_ASCII]))
@throw [OFInvalidFormatException exception];
} @catch (id e) {
[self release];
@throw e;
}
if (!mutable)
|
|
|
<
|
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
self = [OFMutableData alloc];
}
self = [(OFMutableData *)self initWithCapacity: string.length / 3];
@try {
if (!of_base64_decode((OFMutableData *)self,
[string cStringWithEncoding: OFStringEncodingASCII],
[string cStringLengthWithEncoding: OFStringEncodingASCII]))
@throw [OFInvalidFormatException exception];
} @catch (id e) {
[self release];
@throw e;
}
if (!mutable)
|
| ︙ | | | ︙ | |
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
return self;
}
- (void)dealloc
{
if (_freeWhenDone)
free(_items);
[_parentData release];
[super dealloc];
}
- (size_t)count
|
|
|
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
return self;
}
- (void)dealloc
{
if (_freeWhenDone)
OFFreeMemory(_items);
[_parentData release];
[super dealloc];
}
- (size_t)count
|
| ︙ | | | ︙ | |
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
|
return false;
if (memcmp(data.items, _items, _count * _itemSize) != 0)
return false;
return true;
}
- (of_comparison_result_t)compare: (OFData *)data
{
int comparison;
size_t count, minCount;
if (![data isKindOfClass: [OFData class]])
@throw [OFInvalidArgumentException exception];
if (data.itemSize != _itemSize)
@throw [OFInvalidArgumentException exception];
count = data.count;
minCount = (_count > count ? count : _count);
if ((comparison = memcmp(_items, data.items,
minCount * _itemSize)) == 0) {
if (_count > count)
return OF_ORDERED_DESCENDING;
if (_count < count)
return OF_ORDERED_ASCENDING;
return OF_ORDERED_SAME;
}
if (comparison > 0)
return OF_ORDERED_DESCENDING;
else
return OF_ORDERED_ASCENDING;
}
- (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]);
OF_HASH_FINALIZE(hash);
return hash;
}
- (OFData *)subdataWithRange: (of_range_t)range
{
OFData *ret;
if (range.length > SIZE_MAX - range.location ||
range.location + range.length > _count)
@throw [OFOutOfRangeException exception];
|
|
|
|
|
|
|
|
|
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
|
return false;
if (memcmp(data.items, _items, _count * _itemSize) != 0)
return false;
return true;
}
- (OFComparisonResult)compare: (OFData *)data
{
int comparison;
size_t count, minCount;
if (![data isKindOfClass: [OFData class]])
@throw [OFInvalidArgumentException exception];
if (data.itemSize != _itemSize)
@throw [OFInvalidArgumentException exception];
count = data.count;
minCount = (_count > count ? count : _count);
if ((comparison = memcmp(_items, data.items,
minCount * _itemSize)) == 0) {
if (_count > count)
return OFOrderedDescending;
if (_count < count)
return OFOrderedAscending;
return OFOrderedSame;
}
if (comparison > 0)
return OFOrderedDescending;
else
return OFOrderedAscending;
}
- (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]);
OF_HASH_FINALIZE(hash);
return hash;
}
- (OFData *)subdataWithRange: (OFRange)range
{
OFData *ret;
if (range.length > SIZE_MAX - range.location ||
range.location + range.length > _count)
@throw [OFOutOfRangeException exception];
|
| ︙ | | | ︙ | |
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
}
- (OFString *)stringByBase64Encoding
{
return of_base64_encode(_items, _count * _itemSize);
}
- (of_range_t)rangeOfData: (OFData *)data
options: (int)options
range: (of_range_t)range
{
const char *search;
size_t searchLength;
if (range.length > SIZE_MAX - range.location ||
range.location + range.length > _count)
@throw [OFOutOfRangeException exception];
if (data == nil || data.itemSize != _itemSize)
@throw [OFInvalidArgumentException exception];
if ((searchLength = data.count) == 0)
return of_range(0, 0);
if (searchLength > range.length)
return of_range(OF_NOT_FOUND, 0);
search = data.items;
if (options & OF_DATA_SEARCH_BACKWARDS) {
for (size_t i = range.length - searchLength;; i--) {
if (memcmp(_items + i * _itemSize, search,
searchLength * _itemSize) == 0)
return of_range(i, searchLength);
/* No match and we're at the last item */
if (i == 0)
break;
}
} else {
for (size_t i = range.location;
i <= range.length - searchLength; i++)
if (memcmp(_items + i * _itemSize, search,
searchLength * _itemSize) == 0)
return of_range(i, searchLength);
}
return of_range(OF_NOT_FOUND, 0);
}
#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
OFFile *file = [[OFFile alloc] initWithPath: path mode: @"w"];
@try {
|
|
|
|
|
|
|
|
|
|
|
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
}
- (OFString *)stringByBase64Encoding
{
return of_base64_encode(_items, _count * _itemSize);
}
- (OFRange)rangeOfData: (OFData *)data
options: (OFDataSearchOptions)options
range: (OFRange)range
{
const char *search;
size_t searchLength;
if (range.length > SIZE_MAX - range.location ||
range.location + range.length > _count)
@throw [OFOutOfRangeException exception];
if (data == nil || data.itemSize != _itemSize)
@throw [OFInvalidArgumentException exception];
if ((searchLength = data.count) == 0)
return OFRangeMake(0, 0);
if (searchLength > range.length)
return OFRangeMake(OFNotFound, 0);
search = data.items;
if (options & OFDataSearchBackwards) {
for (size_t i = range.length - searchLength;; i--) {
if (memcmp(_items + i * _itemSize, search,
searchLength * _itemSize) == 0)
return OFRangeMake(i, searchLength);
/* No match and we're at the last item */
if (i == 0)
break;
}
} else {
for (size_t i = range.location;
i <= range.length - searchLength; i++)
if (memcmp(_items + i * _itemSize, search,
searchLength * _itemSize) == 0)
return OFRangeMake(i, searchLength);
}
return OFRangeMake(OFNotFound, 0);
}
#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
OFFile *file = [[OFFile alloc] initWithPath: path mode: @"w"];
@try {
|
| ︙ | | | ︙ | |