ObjFW  Diff

Differences From Artifact [e36b8d42d3]:

To Artifact [0640a57f3d]:


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	_OFData_CryptoHashing_reference = 1;
	_OFData_MessagePackParsing_reference = 1;
}

@implementation OFData
@synthesize itemSize = _itemSize;

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

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







|
<

|
<







50
51
52
53
54
55
56
57

58
59

60
61
62
63
64
65
66
	_OFData_CryptoHashing_reference = 1;
	_OFData_MessagePackParsing_reference = 1;
}

@implementation OFData
@synthesize itemSize = _itemSize;

+ (instancetype)dataWithItems: (const void *)items count: (size_t)count

{
	return [[[self alloc] initWithItems: items count: count] autorelease];

}

+ (instancetype)dataWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	return [[[self alloc] initWithItems: items
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
}

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

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

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







|
<

|
<
<







107
108
109
110
111
112
113
114

115
116


117
118
119
120
121
122
123
}

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

- (instancetype)initWithItems: (const void *)items count: (size_t)count

{
	return [self initWithItems: items count: count itemSize: 1];


}

- (instancetype)initWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super init];
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

# 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];








|
<

|
<







187
188
189
190
191
192
193
194

195
196

197
198
199
200
201
202
203

# 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];

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
		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 = of_alloc(1, pageSize);







|
<







228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
		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 = of_alloc(1, pageSize);
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

	return of_range(OF_NOT_FOUND, 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;
	OFStream *stream;

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

	stream = [URLHandler openItemAtURL: URL
				      mode: @"w"];
	[stream writeData: self];

	objc_autoreleasePoolPop(pool);
}

- (OFXMLElement *)XMLElementBySerializing
{
	void *pool;







|
<
<

|
<










<




|
<
<







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

	return of_range(OF_NOT_FOUND, 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;
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
	if (_itemSize != 1)
		@throw [OFInvalidArgumentException exception];

	if (_count <= UINT8_MAX) {
		uint8_t type = 0xC4;
		uint8_t tmp = (uint8_t)_count;

		data = [OFMutableData dataWithItemSize: 1
					      capacity: _count + 2];

		[data addItem: &type];
		[data addItem: &tmp];
	} else if (_count <= UINT16_MAX) {
		uint8_t type = 0xC5;
		uint16_t tmp = OF_BSWAP16_IF_LE((uint16_t)_count);

		data = [OFMutableData dataWithItemSize: 1
					      capacity: _count + 3];

		[data addItem: &type];
		[data addItems: &tmp
			 count: sizeof(tmp)];
	} else if (_count <= UINT32_MAX) {
		uint8_t type = 0xC6;
		uint32_t tmp = OF_BSWAP32_IF_LE((uint32_t)_count);

		data = [OFMutableData dataWithItemSize: 1
					      capacity: _count + 5];

		[data addItem: &type];
		[data addItems: &tmp
			 count: sizeof(tmp)];
	} else
		@throw [OFOutOfRangeException exception];

	[data addItems: _items
		 count: _count];

	[data makeImmutable];

	return data;
}
@end







|
<
<






|
<
<

|
<




|
<
<

|
<



|
<
<





645
646
647
648
649
650
651
652


653
654
655
656
657
658
659


660
661

662
663
664
665
666


667
668

669
670
671
672


673
674
675
676
677
	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];
		[data addItem: &tmp];
	} else if (_count <= UINT16_MAX) {
		uint8_t type = 0xC5;
		uint16_t tmp = OF_BSWAP16_IF_LE((uint16_t)_count);

		data = [OFMutableData dataWithCapacity: _count + 3];


		[data addItem: &type];
		[data addItems: &tmp count: sizeof(tmp)];

	} else if (_count <= UINT32_MAX) {
		uint8_t type = 0xC6;
		uint32_t tmp = OF_BSWAP32_IF_LE((uint32_t)_count);

		data = [OFMutableData dataWithCapacity: _count + 5];


		[data addItem: &type];
		[data addItems: &tmp count: sizeof(tmp)];

	} else
		@throw [OFOutOfRangeException exception];

	[data addItems: _items count: _count];


	[data makeImmutable];

	return data;
}
@end