ObjFW  Diff

Differences From Artifact [86e2a3f40f]:

To Artifact [d34247ee72]:


1
2
3
4

5
6
7
8
9
10
11
1



2
3
4
5
6
7
8
9

-
-
-
+







/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017,
 *               2018, 2019, 2020
 *   Jonathan Schleifer <js@nil.im>
 * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
51
52
53
54
55
56
57
58

59
60
61

62
63
64
65
66
67
68
69
49
50
51
52
53
54
55

56

57

58

59
60
61
62
63
64
65







-
+
-

-
+
-







	_OFData_CryptoHashing_reference = 1;
	_OFData_MessagePackParsing_reference = 1;
}

@implementation OFData
@synthesize itemSize = _itemSize;

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

+ (instancetype)dataWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	return [[[self alloc] initWithItems: items
110
111
112
113
114
115
116
117

118
119
120

121
122
123
124
125
126
127
128
129
106
107
108
109
110
111
112

113

114

115


116
117
118
119
120
121
122







-
+
-

-
+
-
-







}

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

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

- (instancetype)initWithItems: (const void *)items
			count: (size_t)count
		     itemSize: (size_t)itemSize
{
	self = [super init];
193
194
195
196
197
198
199
200

201
202
203

204
205
206
207
208
209
210
211
186
187
188
189
190
191
192

193

194

195

196
197
198
199
200
201
202







-
+
-

-
+
-








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

236
237
238
239
240
241
242
243

244
245
246
247
248
249
250
251
227
228
229
230
231
232
233

234

235
236
237
238
239
240
241







-
+
-







		size_t pageSize;
		unsigned char *buffer;

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

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

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

		pageSize = [OFSystemInfo pageSize];
		buffer = of_alloc(1, pageSize);
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
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







-
+
-
-

-
+
-










-




-
+
-
-








	return of_range(OF_NOT_FOUND, 0);
}

#ifdef OF_HAVE_FILES
- (void)writeToFile: (OFString *)path
{
	OFFile *file = [[OFFile alloc] initWithPath: path
	OFFile *file = [[OFFile alloc] initWithPath: path mode: @"w"];
					       mode: @"w"];

	@try {
		[file writeBuffer: _items
		[file writeBuffer: _items length: _count * _itemSize];
			   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
	[[URLHandler openItemAtURL: URL mode: @"w"] writeData: self];
				      mode: @"w"];
	[stream writeData: self];

	objc_autoreleasePoolPop(pool);
}

- (OFXMLElement *)XMLElementBySerializing
{
	void *pool;
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
702
644
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







-
+
-
-






-
+
-
-

-
+
-




-
+
-
-

-
+
-



-
+
-
-





	if (_itemSize != 1)
		@throw [OFInvalidArgumentException exception];

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

		data = [OFMutableData dataWithItemSize: 1
		data = [OFMutableData dataWithCapacity: _count + 2];
					      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
		data = [OFMutableData dataWithCapacity: _count + 3];
					      capacity: _count + 3];

		[data addItem: &type];
		[data addItems: &tmp
		[data addItems: &tmp count: sizeof(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
		data = [OFMutableData dataWithCapacity: _count + 5];
					      capacity: _count + 5];

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

	[data addItems: _items
	[data addItems: _items count: _count];
		 count: _count];

	[data makeImmutable];

	return data;
}
@end