25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#ifdef OF_HAVE_FILES
# import "OFFile.h"
# import "OFFileManager.h"
#endif
#import "OFStream.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFURL.h"
#import "OFURLHandler.h"
#import "OFXMLElement.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFInvalidServerReplyException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
/* References for static linking */
void
|
|
|
|
|
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#ifdef OF_HAVE_FILES
# import "OFFile.h"
# import "OFFileManager.h"
#endif
#import "OFStream.h"
#import "OFString.h"
#import "OFSystemInfo.h"
#import "OFURI.h"
#import "OFURIHandler.h"
#import "OFXMLElement.h"
#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#import "OFTruncatedDataException.h"
#import "OFUnsupportedProtocolException.h"
/* References for static linking */
void
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#ifdef OF_HAVE_FILES
+ (instancetype)dataWithContentsOfFile: (OFString *)path
{
return [[[self alloc] initWithContentsOfFile: path] autorelease];
}
#endif
+ (instancetype)dataWithContentsOfURL: (OFURL *)URL
{
return [[[self alloc] initWithContentsOfURL: URL] autorelease];
}
+ (instancetype)dataWithStringRepresentation: (OFString *)string
{
return [[[self alloc]
initWithStringRepresentation: string] autorelease];
}
|
|
|
|
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#ifdef OF_HAVE_FILES
+ (instancetype)dataWithContentsOfFile: (OFString *)path
{
return [[[self alloc] initWithContentsOfFile: path] autorelease];
}
#endif
+ (instancetype)dataWithContentsOfURI: (OFURI *)URI
{
return [[[self alloc] initWithContentsOfURI: URI] autorelease];
}
+ (instancetype)dataWithStringRepresentation: (OFString *)string
{
return [[[self alloc]
initWithStringRepresentation: string] autorelease];
}
|
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
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
|
return self;
}
#ifdef OF_HAVE_FILES
- (instancetype)initWithContentsOfFile: (OFString *)path
{
char *buffer = NULL;
unsigned long long size;
@try {
OFFile *file;
size = [[OFFileManager defaultManager]
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
- (instancetype)initWithContentsOfURL: (OFURL *)URL
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
OFURLHandler *URLHandler;
OFStream *stream;
size_t pageSize;
unsigned char *buffer;
if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
@throw [OFUnsupportedProtocolException
exceptionWithURL: URL];
stream = [URLHandler openItemAtURL: URL mode: @"r"];
_count = 0;
_itemSize = 1;
_freeWhenDone = true;
pageSize = [OFSystemInfo pageSize];
buffer = OFAllocMemory(1, pageSize);
|
|
>
|
|
<
<
<
|
|
>
|
<
<
|
<
<
|
>
|
|
<
|
<
<
<
<
<
<
|
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
return self;
}
#ifdef OF_HAVE_FILES
- (instancetype)initWithContentsOfFile: (OFString *)path
{
char *buffer = NULL;
OFStreamOffset fileSize;
@try {
void *pool = objc_autoreleasePoolPush();
OFFile *file = [OFFile fileWithPath: path mode: @"r"];
fileSize = [file seekToOffset: 0 whence: OFSeekEnd];
if (fileSize < 0 || (unsigned long long)fileSize > SIZE_MAX)
@throw [OFOutOfRangeException exception];
[file seekToOffset: 0 whence: OFSeekSet];
buffer = OFAllocMemory((size_t)fileSize, 1);
[file readIntoBuffer: buffer exactLength: (size_t)fileSize];
objc_autoreleasePoolPop(pool);
} @catch (id e) {
OFFreeMemory(buffer);
[self release];
@throw e;
}
@try {
self = [self initWithItemsNoCopy: buffer
count: (size_t)fileSize
freeWhenDone: true];
} @catch (id e) {
OFFreeMemory(buffer);
@throw e;
}
return self;
}
#endif
- (instancetype)initWithContentsOfURI: (OFURI *)URI
{
self = [super init];
@try {
void *pool = objc_autoreleasePoolPush();
OFStream *stream = [OFURIHandler openItemAtURI: URI mode: @"r"];
size_t pageSize;
unsigned char *buffer;
_count = 0;
_itemSize = 1;
_freeWhenDone = true;
pageSize = [OFSystemInfo pageSize];
buffer = OFAllocMemory(1, pageSize);
|
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
- (unsigned long)hash
{
unsigned long hash;
OFHashInit(&hash);
for (size_t i = 0; i < _count * _itemSize; i++)
OFHashAdd(&hash, ((uint8_t *)_items)[i]);
OFHashFinalize(&hash);
return hash;
}
- (OFData *)subdataWithRange: (OFRange)range
|
|
|
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
- (unsigned long)hash
{
unsigned long hash;
OFHashInit(&hash);
for (size_t i = 0; i < _count * _itemSize; i++)
OFHashAddByte(&hash, ((uint8_t *)_items)[i]);
OFHashFinalize(&hash);
return hash;
}
- (OFData *)subdataWithRange: (OFRange)range
|
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
|
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 {
[file writeBuffer: _items length: _count * _itemSize];
} @finally {
[file release];
}
}
#endif
- (void)writeToURL: (OFURL *)URL
{
void *pool = objc_autoreleasePoolPush();
OFURLHandler *URLHandler;
if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil)
@throw [OFUnsupportedProtocolException exceptionWithURL: URL];
[[URLHandler openItemAtURL: URL mode: @"w"] writeData: self];
objc_autoreleasePoolPop(pool);
}
- (OFXMLElement *)XMLElementBySerializing
{
void *pool;
OFXMLElement *element;
if (_itemSize != 1)
@throw [OFInvalidArgumentException exception];
pool = objc_autoreleasePoolPush();
element = [OFXMLElement
elementWithName: self.className
namespace: OFSerializationNS
stringValue: OFBase64Encode(_items, _count * _itemSize)];
[element retain];
objc_autoreleasePoolPop(pool);
return [element autorelease];
}
- (OFData *)messagePackRepresentation
{
OFMutableData *data;
if (_itemSize != 1)
@throw [OFInvalidArgumentException exception];
if (_count <= UINT8_MAX) {
uint8_t type = 0xC4;
uint8_t tmp = (uint8_t)_count;
data = [OFMutableData dataWithCapacity: _count + 2];
[data addItem: &type];
|
|
|
|
|
|
|
<
<
<
<
|
|
>
|
>
|
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
|
range.location + range.length > _count)
@throw [OFOutOfRangeException exception];
if (data == nil || data.itemSize != _itemSize)
@throw [OFInvalidArgumentException exception];
if ((searchLength = data.count) == 0)
return OFMakeRange(0, 0);
if (searchLength > range.length)
return OFMakeRange(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 OFMakeRange(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 OFMakeRange(i, searchLength);
}
return OFMakeRange(OFNotFound, 0);
}
#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
OFFile *file = [[OFFile alloc] initWithPath: path mode: @"w"];
@try {
[file writeBuffer: _items length: _count * _itemSize];
} @finally {
[file release];
}
}
#endif
- (void)writeToURI: (OFURI *)URI
{
void *pool = objc_autoreleasePoolPush();
[[OFURIHandler openItemAtURI: URI mode: @"w"] writeData: self];
objc_autoreleasePoolPop(pool);
}
- (OFXMLElement *)XMLElementBySerializing
{
void *pool;
OFXMLElement *element;
if (_itemSize != 1)
@throw [OFNotImplementedException exceptionWithSelector: _cmd
object: self];
pool = objc_autoreleasePoolPush();
element = [OFXMLElement
elementWithName: self.className
namespace: OFSerializationNS
stringValue: OFBase64Encode(_items, _count * _itemSize)];
[element retain];
objc_autoreleasePoolPop(pool);
return [element autorelease];
}
- (OFData *)messagePackRepresentation
{
OFMutableData *data;
if (_itemSize != 1)
@throw [OFNotImplementedException exceptionWithSelector: _cmd
object: self];
if (_count <= UINT8_MAX) {
uint8_t type = 0xC4;
uint8_t tmp = (uint8_t)_count;
data = [OFMutableData dataWithCapacity: _count + 2];
[data addItem: &type];
|